Click here to Skip to main content
11,432,119 members (56,433 online)
Click here to Skip to main content

Tagged as

Send mail using Google Apps/Gmail Account using C#

, 27 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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

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

Share

About the Author

Mukund Thakker
Technical Lead
India India
Tech 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
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 1 no documentation Pin
Mr President6-Dec-11 11:56
memberMr President6-Dec-11 11:56 
GeneralRe: It is a well managed code.What kind of documentation you nee... Pin
Mukund Thakker6-Dec-11 18:41
memberMukund Thakker6-Dec-11 18:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 27 Apr 2012
Article Copyright 2011 by Mukund Thakker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid