Sending E-mail From C# Class Using G-mail

March 24, 2008 at 8:28 pm 1 comment

Here is a complete C# class that can be used to send mail using a g-mail (Google mail) account. This has been tested as working today.

I’ve used the web.config file to get authentication parameters and smtp server, but these can be replaced directly with strings if you like. Those strings will be:

SmtpUser: Your gmail user name. SmtpPassword: Your gmail password. SmtpClient: smtp.gmail.com

Also worth noting is that the port used is 587 instead of 465. If 587 doesn’t work, try 465.

The Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Net;

///
/// Summary description for MailSender
///
public class MailSender
{

    // Constructor #1 for general text only messages
    ///
    /// Constructor for basic email sending, does not required that IsHtml bool is specified.
    ///
    ///
    ///
    ///
    ///
    public MailSender(string from, string to, string subject, string body)
    {
        m_from = from;
        m_to = to;
        m_subject = subject;
        m_body = body;

        SendMailGmail(m_from, m_to, m_subject, m_body, false);
    }

    // Constructor #2 with ability to specify isHtml as true for sending HTML email
    ///
    /// Constructor with optional IsHtml parameter for sending of HTML email.
    ///
    ///
    ///
    ///
    ///
    ///
    public MailSender(string from, string to, string subject, string body, bool isHtml )
    {
        m_from = from;
        m_to = to;
        m_subject = subject;
        m_body = body;

        SendMailGmail(m_from, m_to, m_subject, m_body, isHtml);
    }

    ///
    /// Send email method configured for use with gmail.
    ///
    /// From
    /// To
    /// Subject
    /// Body text
    /// IsHtml
    private void SendMailGmail(string from, string to, string subject, string body, bool isHtml)
    {
        // Code from: http://www.andreas-kraus.net/blog/aspnet-20-aka-systemnetmail-with-gmail/

        System.Net.Mail.MailMessage message = new MailMessage(from, to, subject, body);
        message.Priority = MailPriority.High;
        message.IsBodyHtml = isHtml;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = m_smtpClient;

        smtp.Credentials = new System.Net.NetworkCredential(m_smtpUserName, m_smtpPassWord);
        smtp.EnableSsl = true;
        // Not the usual port but works
        smtp.Port = 587;
        smtp.Timeout = 10;

        try
        {
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            // Send error but pass false to prevent looping error emails
            ErrorLogger errorLogger = new ErrorLogger(ex, false);
        }
    }

    private string m_from = "";
    private string m_to = "";
    private string m_subject = "";
    private string m_body = "";

    private string m_smtpClient = ConfigurationManager.AppSettings["SmtpClient"];
    private string m_smtpUserName = ConfigurationManager.AppSettings["SmtpUser"];
    private string m_smtpPassWord = ConfigurationManager.AppSettings["SmtpPassword"];
}
Advertisements

Entry filed under: ASP.NET, C#. Tags: , , , , .

Porting Visual Studio .mdf Database File to SQL Server – ASP.NET

1 Comment Add your own

  • 1. Rohit Shrivastava  |  July 5, 2008 at 12:05 pm

    Send HTML mails Is easy but how to Read The html mails can u tell me…

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2008
M T W T F S S
    May »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Most Recent Posts


%d bloggers like this: