Setting up SMTP is sometimes not easy. Last year I was developing a photo social network (like Flickr) and we needed a registration confirmation system through email. The SMTP settings of the webhost on which we were testing really sucked so I dumped their SMTP and installed Google Apps for our domain. GMail provides easy SMTP access for sending emails and if you have configured your domain’s email at Google apps then I think its best to use for any email scenarios.

In this tutorial I will tell you how you can easily use GMail for sending emails in ASP.NET (C#). I have used my ProgrammerFish Google Apps email account but you can also use any GMail account. The final webpage looks like this:

image

So here is how to do it:

Go to File>new>Website and name this project GmailMailSender ( or whatever you like) and press ok

image

Now let’s create some UI for the web page. double click default.aspx in the solution explorer. And click Design button.

Create an interface like this. Insert 4 text boxes named as tb_GmailAccount, tb_GmailPassword (with ‘TextMode’ property as “password” ), tb_RecieverEmail and tb_Message(with ‘TextMode’ property as “multiline”) . Also insert 2 labels named as lb_error (with ‘Text’ property as “Error sending mail” and ‘Visible’ property as “false”) bt_sendMail and lb_MailSent(with ‘Text’ property as “Mail Sent”with ‘Visible’ property as “false”)

image

Now Solution Explorer, right click on the Web Project Node and click on Add New Item

image

In Visual Studio Installed Templates select Class. Name it as GmailSender.cs and click Add. If a dialog box appears then click Yes

image

Add the following code in that class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for GmailSender
/// </summary>
public class GmailSender
{
    public GmailSender()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
    {
        try
        {
            NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(gMailAccount);
            msg.To.Add(new MailAddress(to));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;
            client.Send(msg);

            return true;
        }
        catch (Exception)
        {
            return false;
        }

    }
}

Now open Default.aspx and double click Send Mail button to open bt_sendMail_click event function code.

image Add this code in the class:

protected void bt_sendMail_Click(object sender, EventArgs e)
    {
        if (GmailSender.SendMail(tb_GmailAccount.Text, tb_GmailPassword.Text, tb_RecieverEmail.Text, tb_Subject.Text, tb_Message.Text))
        {
            lb_MailSent.Visible = true;

        }
        else
        {
            lb_error.Visible = false;
        }
    }

Press F5 to run it if Debugging not enabled dialog box appears click OK

As ProgrammerFish.com is using Google Apps for email, I will use my ProgrammerFish email address to send an email.

image If mail is sent successfully, it will show email sent label, other wise it will show Error sending mail label, Lets check it out if its sent or not, here it is!

image

Download the solution file: GMailMailSender.zip