How can I send email using Gmail SMTP in asp.net mvc application?

I found a very good article on a website https://askgif.com about using Gmail SMTP with C#, so am sharing with you : https://askgif.com/blog/122/seding-email-using-gmail-smtp-in-asp-net-mvc-application/

Create Gmail Class comprises of all needed data type and member function as below

public class GMailer
{
    public static string GmailUsername { get; set; }
    public static string GmailPassword { get; set; }
    public static string GmailHost { get; set; }
    public static int GmailPort { get; set; }
    public static bool GmailSSL { get; set; }

    public string ToEmail { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public bool IsHtml { get; set; }

    static GMailer()
    {
        GmailHost = "smtp.gmail.com";
        GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
        GmailSSL = true;
    }

    public void Send()
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Host = GmailHost;
        smtp.Port = GmailPort;
        smtp.EnableSsl = GmailSSL;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

        using (var message = new MailMessage(GmailUsername, ToEmail))
        {
            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = IsHtml;
            smtp.Send(message);
        }
    }
}

Then just used the following code wherever you want to send the email to the required email account.

GMailer.GmailUsername = "[email protected]";
        GMailer.GmailPassword = "YourPassword";

        GMailer mailer = new GMailer();
        mailer.ToEmail = "[email protected]";
        mailer.Subject = "Verify your email id";
        mailer.Body = "Thanks for Registering your account.<br> please verify your email id by clicking the link <br> <a href='youraccount.com/verifycode=12323232'>verify</a>";
        mailer.IsHtml = true;
        mailer.Send();

Hope this will help you. Mark as answer if this helps you.


Here is a Email class that can be used with ASP.NET MVC4 leveraging dependency injection. A full running sample application & unit tests that use this class can be found here in my github space https://github.com/fredo007/i6technology/tree/master/InsuranceSales.

I've also put together an article explaining methodology & use here http://prestoasp.net/how-to-send-email-using-gmail-smtp-in-an-asp-net-mvc-application/

public class GmailEmailService : IEmailService
{
    private readonly SmtpConfiguration _config;

    private const string GmailUserNameKey = "GmailUserName";
    private const string GmailPasswordKey = "GmailPassword";
    private const string GmailHostKey = "GmailHost";
    private const string GmailPortKey = "GmailPort";
    private const string GmailSslKey = "GmailSsl";

    public GmailEmailService()
    {
        _config = new SmtpConfiguration();
        var gmailUserName = ConfigurationManager.AppSettings[GmailUserNameKey];
        var gmailPassword = ConfigurationManager.AppSettings[GmailPasswordKey];
        var gmailHost = ConfigurationManager.AppSettings[GmailHostKey];
        var gmailPort = Int32.Parse(ConfigurationManager.AppSettings[GmailPortKey]);
        var gmailSsl = Boolean.Parse(ConfigurationManager.AppSettings[GmailSslKey]);
        _config.Username = gmailUserName;
        _config.Password = gmailPassword;
        _config.Host = gmailHost;
        _config.Port = gmailPort;
        _config.Ssl = gmailSsl;
    }

    public bool SendEmailMessage(EmailMessage message)
    {
        var success = false;
        try
        {
            var smtp = new SmtpClient
            {
                Host = _config.Host,
                Port = _config.Port,
                EnableSsl = _config.Ssl,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(_config.Username, _config.Password)
            };

            using (var smtpMessage = new MailMessage(_config.Username, message.ToEmail))
            {
                smtpMessage.Subject = message.Subject;
                smtpMessage.Body = message.Body;
                smtpMessage.IsBodyHtml = message.IsHtml;
                smtp.Send(smtpMessage);
            }

            success = true;
        }
        catch (Exception ex)
        {
            //todo: add logging integration
            //throw;
        }

        return success;
    }
}