Sending mail programmatically using C# 2.0 with Gmail
The following snippet derived from a forum posting makes use of the all new
System.Net.Mail namespace found in the .NET 2.0 Framework.
<script runat="server">
protected string now = DateTime.Now.ToString();
public void Page_Load(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("yourid@gmail.com", "yourpassword");
mail.To.Add("acme@acme.com");
mail.Subject = "subject goes here";
mail.From = new System.Net.Mail.MailAddress(yourid@gmail.com);
mail.IsBodyHtml = true;
mail.Body = "This message has been sent programmatically with GMail";
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);
}
</script>
The
System.Net.Mail FAQ contains interesting facts and numerous practical snippets.
Code formatted with
JTidy.de