HOW TO send an email programmatically using C# with GMail

This is derived from a forum posting.

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("yourid@gmail.com", "yourpwd");

mail.To.Add("acme@acme.com");
mail.Subject = "subject";

mail.From = new System.Net.Mail.MailAddress("yourid@gmail.com");
mail.IsBodyHtml = true;
mail.Body = "message";

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = cred;
smtp.Port = 587;
smtp.Send(mail);


System.Net.Mail is the namespace used to send email if you are using the 2.0 .NET Framework.

Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop.


The System.Net.Mail FAQ contains numerous practical snippets


Related link: HOW TO send an email with a Word or Excel file attachment built on the fly

Comments

Post a Comment