Sunday, February 21, 2010

C# - send email by SmtpClient using Gmail SMTP


...
using System.Net.Mail;
using System.Net;
...
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@sampleApp.com");
mail.To.Add("you@sampleApp.com");

//set the content
mail.Subject = "C# mail - Sent by System.Net.Mail";
mail.Body = "this is the body content of the email.";
mail.BodyEncoding = UTF8Encoding.UTF8;

// Delivery notification setting
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

// SMTP client settings
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.Timeout = 10000;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("iborn2code", "farhad2006");

//send the message
smtp.Send(mail);
...

Share/Bookmark

No comments: