Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Tip/Trick

Send mail using Google Apps/Gmail Account using C#

Rate me:
Please Sign up or sign in to vote.
3.40/5 (9 votes)
27 Apr 2012CPOL 46.2K   16   8
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)


Written By
Technical Lead
India India
Project Lead
MCTS - .NET Framework 4.0, Web Applications

Blog : http://thakkermukund.wordpress.com
Twitter@thakkermukund

Don't code today, what you can't debug tomorrow!
Everything makes sense in someone's mind

Comments and Discussions

 
GeneralMy vote of 5! Pin
Foosfam28-Aug-14 12:57
Foosfam28-Aug-14 12:57 
QuestionIt doesn't work on my system Pin
Steve 2628774181-Jul-13 20:30
Steve 2628774181-Jul-13 20:30 
AnswerRe: It doesn't work on my system Pin
Mukund Thakker4-Jul-13 21:26
professionalMukund Thakker4-Jul-13 21:26 
GeneralMy vote of 2 Pin
Tim Corey30-Apr-12 5:58
professionalTim Corey30-Apr-12 5:58 
GeneralReason for my vote of 1 no documentation Pin
Mr President6-Dec-11 10:56
Mr President6-Dec-11 10:56 
GeneralRe: It is a well managed code.What kind of documentation you nee... Pin
Mukund Thakker6-Dec-11 17:41
professionalMukund Thakker6-Dec-11 17:41 
It is a well managed code.What kind of documentation you needed?

GeneralI am getting following error. Send Email Failed.Request for ... Pin
Mike Thom1-Dec-11 7:59
Mike Thom1-Dec-11 7:59 
GeneralRe: set following values UseDefaultCredentials="false" EnableSsl... Pin
Mukund Thakker1-Dec-11 17:40
professionalMukund Thakker1-Dec-11 17:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.