Sending mail through GMail SMTP server


Author
Message
Trent Taylor
Trent Taylor
StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)StrataFrame Developer (8.5K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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);

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search