StrataFrame Forum

Sending mail through GMail SMTP server

http://forum.strataframe.net/Topic24574.aspx

By Trent L. Taylor - 9/8/2009

If you ever have the need to send mail through a GMail account, this sample may save you a few minutes. It isn't hard, but there are a few differences which require SSL and authentication.



//-- Establish Locals

SmtpClient smtp = new SmtpClient("smtp.gmail.com ", 587);

MailMessage msg = new MailMessage("reply@somewhere.com", "to@somewhere.com");



//-- Set the subject line

msg.Subject = "Hello World";



//-- Set the body (Plain text in this sample)

msg.Body = "Here is the body of the message.";



//-- Attach the file if necessary

msg.Attachments.Add(new Attachment(pathAndFileName));



smtp.EnableSsl = true;

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.UseDefaultCredentials = false;

smtp.Credentials = new NetworkCredential("myemail@gmail.com", "mypassword");



//-- Send the message

smtp.Send(msg);