Sending email in asp.net is very useful and customizable too. The powerful classes help you compose and send email in minute! The glamour has always fascinated me, but more important is the frequent questions that are asked in asp.net related forums on sending email in asp.net using C# or vb.net. We can send email using some smtp server. And if this is not viable (sometimes we don't have smtp accounts!) we can use our own gmail account to send email (Thank you gmail!)
Take one example- Here goes the question that was asked in asp.net forum-
"can someone give me code how to send an e-mail from c# code
I am working with a company using Microsoft outlook for my mails.
I want to send mail only to 1 person, also please help me in identifying "smtp hostname""
Here goes the simple and helpful answer.
Visiting all above listed links you must have realized that sending email in asp.net is quite fun and useful too. In fact, a lot of customizations exist depending on your requirement. My suggestion is to do the basic email sending first. After that you can play with the classes and objects to perform more actions. Let me show you the basic email sending using c#.
//sending basic mail in asp.net
public void SendBasicMail()
//create new mailmessage object
MailMessage message = new MailMessage(new MailAddress("firstname.lastname@example.org"), new MailAddress("email@example.com"));
message.Body = "Hi. This is test message.";
message.Subject = "Test Message!";
message.IsBodyHtml = false;
//create smtp client and send mail
SmtpClient client = new SmtpClient("mail.mysmtpserver.com", 25);
lblError.Text = "Email sent successfully!";
lblError.ForeColor = Color.Green;
catch (Exception ex)
lblError.Text = "Email could not be sent. " + ex.Message;
lblError.ForeColor = Color.Red;
This simple example will help you understand the basics of sending email in asp.net web application.
protected void sendMail()
string from = "firstname.lastname@example.org"; //Replace this with your own correct Gmail Address
string to = "email@example.com"; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "Password");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
catch (Exception ex)
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
} // end try
client.Credentials = new System.Net.NetworkCredential(from, "Password"); in this statement... write your gmail password.... from address should be a gmail address....
Hope this post will help. Thank you. Happy Programming!