Click here to Skip to main content
12,820,156 members (24,362 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


16 bookmarked
Posted 29 Nov 2011

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) && 
                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(
        myMail.IsBodyHtml = true;
        myMail.Body = strBody;
            return true;
        catch (Exception ex)
            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=""/>
<add key="UseDefaultCredentials" value="false"/>
<add key="MailUserName" value=""/>
<add key="MailPassword" value="xxxxxx"/>
<add key="MailPort" value=""/>
<add key="EnableSsl" value="True"/>
<add key="sendBccTest" value="False" />
<add key="SiteUrl" value=""/> 


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


About the Author

Mukund Thakker
Technical Lead
India India
Project Lead
MCTS - .NET Framework 4.0, Web Applications

Blog :

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

You may also be interested in...


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

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