Monday, June 1, 2009

How to send email from Basic C# code to send simple email

Sending email in 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 related forums on sending email in using C# or 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 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 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 
public void SendBasicMail()
//create new mailmessage object
MailMessage message = new MailMessage(new MailAddress(""), new MailAddress(""));
message.Body = "Hi. This is test message.";
message.Subject = "Test Message!";
message.IsBodyHtml = false;

//create smtp client and send mail
SmtpClient client = new SmtpClient("", 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 web application.

Now interested in sending email using our own gmail account? Here goes the coding by getchinna_sv (Thank you getchinna_sv! Yours sangam100).

 protected void sendMail()
string from = ""; //Replace this with your own correct Gmail Address
string to = ""; //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 = "";
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!

kick it on Shout it pimp it


Udit Dawra said...

instead of using a gmail id.,.,i have a domain name and want to send email using that email id.,.,

Sangam Uprety said...

Dear Udit Dawra,
You can visit to send email using your own domain, user name and password.

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles