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="you@domain.com">
        <network host="maiServerl" password="password" 
           userName="loginName" port="25"/>
    </smtp>
</mailSettings>

You can use

<mailSettings>
    <smtp from="you@domain.com"
         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);

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Templated E-Mail using .NETTemplated E-Mail using .NET One thing I’m pretty consistent about is letting the computer do most of my work for me.  As a “programmer” I really don’t like to program.  I prefer to solve problems. You’ve […]
  • Find and report unhandled exceptionsFind and report unhandled exceptions OK.. I don't have time to try this right now.  But, if this works even a little bit, it is probably worth the $300 they want for it.Find and report unhandled exceptions - Exception […]
  • Managing EmailManaging Email I recently started using a product called Clear Context that does a much better job of integrating Outlook's email with Outlook's calendar. The two things I like best about it are that it […]
  • Determine The Role of a User in ASP.NETDetermine The Role of a User in ASP.NET There are several controls that allow you to display content based on the role a user is in, including: - LoginView - LoginStatus And the web.config file allows us to control which […]
  • ASP.NET Session Variables Not StickingASP.NET Session Variables Not Sticking I’ve stumbled across this problem twice in the last couple of months so I figure it is about time I blogged about it. The situation is that you have  a page on your web site that […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

One Pingback/Trackback