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

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("your_account@gmail.com", "yourpassword");
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;                
 
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("your_account@gmail.com");
                msg.To.Add(new MailAddress("destination_address@someserver.com"));
 
                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
  • 515 Users Found This Useful
Was this answer helpful?

Related Articles

How do I set up my email in Outlook Express?

The following is a walk-through for setting up Outlook Express. (a) Go to 'Tools' ->...

Do you support IMAP?

IMAP stands for Internet Message Access Protocol. IMAP is a method of accessing electronic mail...

How to Setting up email for Blackberry

Things you will need: • Your domain name. • Your Email address username@mt-example.com...

I have a mailing list, how many emails can I send out?

Yes. There is a limit of a few hundred recipients per mailing on email sent via our shared...

I need to share larger than 5MB binary files, what can I do?

If you need to share binary files which are larger than the 5MB-5.5MB size supported as email...