Click here to Skip to main content
Click here to Skip to main content

Send mail using Google Apps/Gmail Account using C#

By , 27 Apr 2012
 

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)

About the Author

Mukund Thakker
Software Developer (Senior)
India India
Member
Software Developer
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 1 no documentationmemberMr President6 Dec '11 - 10:56 
GeneralRe: It is a well managed code.What kind of documentation you nee...memberMukund Thakker6 Dec '11 - 17:41 

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

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