Test Sending Email without a Server in ASP.NET

back-041 By now, most people are familiar with the fact that ASP.NET will send mail from the codebehind by simply adding a few lines to your web.config file and adding another few lines of code in the codebehind file.

But it wasn’t until recently that I found that you don’t need to have access to an SMTP server to test your code.

In fact, this little trick will allow you to read the email without clogging up your email client with email you only wanted for testing purposes.

Instead of the normal entry of

<mailSettings>
    <smtp from="[email protected]">
        <network host="maiServerl" password="password"
           userName="loginName" port="25"/>
    </smtp>
</mailSettings>

You can use

<mailSettings>
    <smtp from="[email protected]"
         deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory
            pickupDirectoryLocation="c:\mail"/>
    </smtp>
</mailSettings>

This will drop your email message in the c:\mail directory as an *.eml file which you can open with Outlook Express.

The code you would write to send the mail is still the same:

SmtpClient smtp = new SmtpClient();
MailAddress from = new MailAddress(fromEmail, fromEmail);
MailAddress to = new MailAddress(emailAddress, emailAddress);
MailMessage message = new MailMessage(from, to);
message.Subject = SubjectLine;
message.Body = htmlString;
message.From = from;
message.To.Add(to);
message.IsBodyHtml = true;
smtp.Send(message);

Related Post

One Response to “Test Sending Email without a Server in ASP.NET”