How to send HTML Email from ASP.NET using your Gmail account?

 Copyright © 2007-2012 www.AspDotNetFaq.com


Here is an example on how to send HTML email from your ASP.NET page using your Google account.

(This setup can be easily used to send messages via any other SMTP server that requires authentication).

Note: the code snippet assumes you have a Label component on Page with named lblMsg that will show
success/failure message to the user that is sending email.
(But this can be easily changed).

                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
 
                // setup Smtp authentication
                System.Net.NetworkCredential credentials = 
new
System.Net.NetworkCredential("[email protected]", "yourpassword");
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;                
 
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("[email protected]");
                msg.To.Add(new MailAddress("[email protected]"));
 
                msg.Subject = "This is a test Email subject";
                msg.IsBodyHtml = true;
                msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");
 
                try
                {
                    client.Send(msg);
                    lblMsg.Text = "Your message has been successfully sent.";
                }
                catch (Exception ex)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "Error occured while sending your message." + ex.Message;
                }


Just make sure you dont overuse it, Google is our friend



 Copyright © 2007-2012 www.AspDotNetFaq.com