Sending mail with ASP.NET and Gmail

After a server upgrade I had to setup my mail again. It seems that IIS7 has lost it’s core mail functionality, so I tried to use the SMTP service from Gmail for sending my e-mail. First thing I did is to setup a simple test page, that I would like to share with you.

Front

The page contains a simple web form for inserting some test data and is looking like this:

 

Knipsel

 
 
 
 

The markup code

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gmail_mailtest.aspx.cs" Inherits="SendMail" %>
<html>
<head runat="server"><title>Email Test Page</title></head>
<body>
    <form id="form1" runat="server">
        Message to: <asp:TextBox ID="txtTo" runat="server" Text="[your_mail]" /><br>
        Message from: <asp:TextBox ID="txtFrom" runat="server" Text="[yourname]@gmail.com" /><br>
        Subject: <asp:TextBox ID="txtSubject" runat="server" Text="test from mailtest.aspx" /><br>
        Message Body:<br>
        <asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px" Text="test123" /><br>
        <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email via Gmail" /><br>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Code behind

The code behind is handling the connection to Gmail:

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
            txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;
        smtpClient.Credentials = new System.Net.NetworkCredential("[yourname, without the @gmail.com]","[PaSSWoRD]");
        smtpClient.EnableSsl = true;

        try
        {
           smtpClient.Send(mailObj);
           Label1.Text = "Mail is sent!";
        }
            catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }
}
 
Tadaa, it works like a charm!

Developing with DevNullSmtp mailserver

When you are developing a .NET website with e-mail functionality you often want to test the mails your code is sending. Often you can use a SMTP mail server within the domain, but you’re always have to worry about not sending test e-mail to the outside world. Alternatively you could setup your own mail server somewhere, but getting everything in place and configured is not always easy.

Lately I started using DevNullSmtp mailserver for developing mail applications. It’s purpose is simple: catch the mail sent to port 25, log the message and destroy it (hence the name DevNull). It has a graphical user interface and also command line options. Btw, you need Java runtime installed on your PC to run DevNullSmtp. The GUI:

smtpdevnull

 

You can see that I use the default SMTP port 25. After sending mails, you can use the four tabs to inspect the mail actions. You can view the SMTP logs for mail instructions or just keep an eye on the last message (like shown above).

You can see that the mail is a multipart mail which contains rich HTML markup. To check the mail in a mail program like Outlook, you have the option to save incoming message in a folder as .eml files:

DevNullSmtp2

This is a very useful options to verify the look and feel of HTML e-mail. I always have this check enabled via a batch file start.cmd :

"C:\Program Files (x86)\Java\jre6\bin\java" -jar DevNullSmtp.jar -p 25 -s emails

Have fun e-mailing,