Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Send mail using Google Apps/Gmail Account using C#

3.40/5 (9 votes)
27 Apr 2012CPOL 47.5K  
How to send emails using Google Apps/Gmail Account

You can send Mail using your Gmail account using following code.

Add clsCommon.cs class file in App_Code folder.

C#
public class clsCommon
{
    public clsCommon()
    {
        //
        // TODO: Add constructor logic here
        //
    }
 
    public static bool sendEmail(string strSubject, string strFrom, 
           string strTo, string strBody, string strCc, 
           string strBcc,string displayName)
    {
        System.Net.Mail.SmtpClient Mail = new System.Net.Mail.SmtpClient();
        Mail.Host = clsCommon.value("SMTPServer").ToString();
 

        string username = null;
        string Password = null;
        if (clsCommon.value("UseDefaultCredentials") + "" == "false")
        {
            Mail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            username = clsCommon.value("MailUserName");
            Password = clsCommon.value("MailPassword");
            System.Net.NetworkCredential basicAuthenticationInfo = 
              new System.Net.NetworkCredential(username, Password);
            if ((clsCommon.value("MailPort") != null) && 
                (isNumeric(clsCommon.value("MailPort"))))
            {
                Mail.Port = Convert.ToInt16(clsCommon.value("MailPort"));
            }
            Mail.UseDefaultCredentials = false;
            Mail.Credentials = basicAuthenticationInfo;
            if (clsCommon.value("EnableSsl") == "True")
                Mail.EnableSsl = true;
        }
 
        System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
        myMail.Subject = strSubject;
 
        myMail.From = new System.Net.Mail.MailAddress(strFrom,displayName);
        myMail.To.Add(new System.Net.Mail.MailAddress(strTo));
        if (!string.IsNullOrEmpty(strCc))
        {
            myMail.CC.Add(new System.Net.Mail.MailAddress(strCc));
        }
        if (!string.IsNullOrEmpty(strBcc))
        {
            myMail.Bcc.Add(new System.Net.Mail.MailAddress(strBcc));
        }
 
        if (clsCommon.value("sendBccTest") == "True")
            myMail.Bcc.Add(new System.Net.Mail.MailAddress(
            clsCommon.value("sendBccTestEmailAddress")));
 
        myMail.IsBodyHtml = true;
        myMail.Body = strBody;
        try
        {
            Mail.Send(myMail);
 
            return true;
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
            return false;
 
        }
    }
 
    public static string value(string key)
    {
        if ((ConfigurationManager.AppSettings[key] != null))
        {
            return ConfigurationManager.AppSettings[key];
        }
        return ConfigurationManager.AppSettings["SiteUrl"];
    }
}  

Add following entries in web.config file

XML
<add key="SMTPServer" value="smtp.gmail.com"/>
<add key="UseDefaultCredentials" value="false"/>
<add key="MailUserName" value="xxxx@gmail.com"/>
<add key="MailPassword" value="xxxxxx"/>
<add key="MailPort" value=""/>
<add key="EnableSsl" value="True"/>
<add key="sendBccTest" value="False" />
<add key="SiteUrl" value="http://www.website.com"/> 
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)