• Uncategorized

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).

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

public class Gmail
{
    /// <summary>
    /// Sends the mail.
    /// </summary>
    public string SendMail(string toEmail, string subject, string body)
    {
        string returnMsg = string.Empty;
        SmtpClient client = new SmtpClient(); 
        client.DeliveryMethod = SmtpDeliveryMethod.Network; 
        client.EnableSsl = true; 
        client.Host = "smtp.gmail.com"; 
        client.Port = 587;                
        // setup Smtp authentication               
        NetworkCredential credentials = 
            new 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(toEmail));     
        msg.Subject = subject;          
        msg.IsBodyHtml = true;
        msg.Body = string.Format(body);  
        try { 
            client.Send(msg);
            returnMsg = "Message has been successfully sent."; 
        }
        catch (Exception ex) 
        { 
            
            returnMsg = "Error occured while sending message." 
                    + ex.Message; 
        }
        return returnMsg;

    }
}

Backtrack: http://www.aspdotnetfaq.com/Faq/How-to-send-HTML-Email-from-ASP-NET-using-your-Gmail-account.aspx

avatar

Deepak Kamboj

Deepak Kamboj is a Solution Architect and Technology Enthusiast, located at Redmond, WA, having 14+ years of hands on experience in the IT industry.

You may also like...