Wednesday, June 24, 2009

Quick Mail Debug Page

I recently helped a client debug some mail issues in an ASP.NET application.  These issues can be particularly frustrating because there are many different points of failure ranging from users' mail client, spam filters, SMTP setup, and more.  In this particular case, no errors occurred in the application- smtpClient.Send exited successfully.  However mail was not being received.  Somewhere between SmtpClient and the users' Outlook, the mail was disappearing! After trying several different settings, I finally wrote this quick mail test page:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Net.Mail"%>
<%@ Import Namespace="System.Net"%>
<script runat="server">
    protected void cmdSend1_Click(object sender, EventArgs e)
        var client = new SmtpClient();
        client.Send(this.uiFrom1.Text, this.uiTo1.Text, "Test - " + DateTime.Now.ToString("g"), "This is a test from the MailTest.aspx form.");
    protected void cmdSend2_Click(object sender, EventArgs e)
        var client = new SmtpClient(this.uiServer.Text);
        client.Credentials = new NetworkCredential(this.uiUser.Text, this.uiPassword.Text);
        client.Send(this.uiFrom2.Text, this.uiTo2.Text, "Test - " + DateTime.Now.ToString("g"), "This is a test from the MailTest.aspx form.");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <b>Send a Simple Test</b><br />
        Tests sending a simple message using the default settings from web.config<br />
        To Address<br />
        <asp:TextBox ID="uiTo1" runat="server"></asp:TextBox>
        <br />
        From Address<br />
        <asp:TextBox ID="uiFrom1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="cmdSend1" runat="server" onclick="cmdSend1_Click" Text="Send" />
        <br />
        <br />
        <b>Test Mail Settings</b><br />
        Tests sending a message using settings other than the ones in web.config<br />
        To Address<br />
        <asp:TextBox ID="uiTo2" runat="server"></asp:TextBox>
        <br />
        From Address<br />
        <asp:TextBox ID="uiFrom2" runat="server"></asp:TextBox>
        <br />
        Server<br />
        <asp:TextBox ID="uiServer" runat="server"></asp:TextBox>
        <br />
        Username<br />
        <asp:TextBox ID="uiUser" runat="server"></asp:TextBox>
        <br />
        Password<br />
        <asp:TextBox ID="uiPassword" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="cmdSend2" runat="server" onclick="cmdSend2_Click" Text="Send" />
        <br />

This let me quickly test both the configured mail settings, and other settings.  After a few tries, I discovered that the 'from' address being used was no longer valid (for reasons I still don't completely understand).  A little configuration change, and the app worked fine!

To use, simply copy the code above into a file named MailTest.aspx in the problem app.  Browse to it and it should display a form that lets you test the various mail settings. Depending on your app, this is much quicker than changing settings in web.config and running through your app over and over.

No comments: