ASP.NET how to prevent sending emails while developing

by Cliff 23. May 2009

There are times when developing that I want to be able to see what the contents will be for emails generated from my application, but I do not want the emails to actually go to the reciepients.  In addition I may not have my local development machine set up to relay mail.

Sometime, updating the development system email addresses to a generic address makes sense.  Other times, I do not or cannot do this, so instead, I opt for the following technique.

With this approach, you add a configuration setting to your config file, and the result is that the email will be placed in a directory of your choosing, but will not actually be sent.

To accomplish this, add the below setting to your config file:

 <system.net>
    <mailSettings>
      <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="C:\All\Maildrop" />
      </smtp>
    </mailSettings>    
  </system.net>

 Now, when you send your email using code similar to below, the email will appear in the directory you specified.

 MailMessage message = new MailMessage();
 message.From = new MailAddress(...

... other code to set to, body goes here

SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);

 

Related posts

Add comment


(will show your Gravatar icon)  

  Country flag




Live preview

March 12. 2010 00:21

Gravatar

Cliff Gray's Info

Cliff Gray
Developer/Founder GrayTechnology.com.

E-mail me Send mail

Authors

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Blogroll

Download BlogEngine.NET

Download at CodePlex

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Subscribe