Sending Email in a Development Environment without an SMTP Server

By admin on Jan 08 2008 | 20 Comments

Sometimes I'm not at a client location and I cannot check to make sure that an email component is working correctly. This results from not having the email server at my disposal to relay the message off of. I also run Vista, which does not come with an SMTP server.

 

Therefore my setting in the App.config wont work for development.

Here's what the settings look like at the client location:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="mail.example.com"/>
      </smtp>
    </mailSettings>
  </system.net>

 

So, while I'm developing remotely I will change the configuration to this 

  <system.net>
    <
mailSettings>
      <
smtp deliveryMethod="SpecifiedPickupDirectory">
        <
specifiedPickupDirectory pickupDirectoryLocation="c:\temp\maildrop\"/>
      </
smtp>
    </
mailSettings>
  </
system.net>

 

What this setting will do is create the email file and drop it into the c:\temp\maildrop\ folder. This setting can be used if you have your SMTP server watch a directory for new mail and it will then send it, or you can use it like I am - for testing when I need to view the email without actually sending it.

Now, when I send an email, like this:

 

        [Test]
        public void SendMail()
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage mailMessage = new MailMessage("from@example.com", "to@example.com");
            mailMessage.Subject = "This is a test";
            mailMessage.Body = "This is the body";
            smtpClient.Send(mailMessage); 
        }

 

The mail message will get generated to c:\temp\maildrop\ . Visit this location with Explorer and you'll see something similar to this:

image

 

Now open this .eml file with any text editor (I prefer Notepad++) and you will see the contents of your email.

 

Email Contents

x-sender: from@example.com
x-receiver: to@example.com
mime-version: 1.0
from: from@example.com
to: to@example.com
date: 7 Jan 2008 18:24:10 -0700
subject: This is a test
content-type: text/plain; charset=us-ascii
content-transfer-encoding: quoted-printable

This is the body

 

Now you can test the email functionality of your application without actually having a SMTP Server.

Note: If  you're using ASP.NET you will need to allow ASP.NET to write to the pickupDirectoryLocation.

Post info

Tags:
Categories: .NET , ASP.NET

Comments

Josh Stodola
Josh Stodola on 1/16/2008 10:40 PM Awesome!  Thanks for sharing, I had no idea about this.
HelloWorld Vista
HelloWorld Vista on 3/8/2008 10:09 PM How would you write the same code in vb.net 2005?
Please suggest -Thanks
Donn Felker
Donn Felker on 3/8/2008 11:35 PM @HelloWorld Vista:

You can easily transform this to VB.NET by using a converter as found via a Google Search. Here is an example: labs.developerfusion.co.uk/.../csharp-to-vb.aspx
James Eggers
James Eggers United States on 9/10/2008 10:00 PM Very nice post.  I have projects going on right now where this would be invaluable.  Thanks
trackback
DotNetKicks.com on 9/11/2008 3:29 AM Trackback from DotNetKicks.com

Sending Email in a Development Environment without an SMTP Server
Kevin Pang
Kevin Pang United States on 9/11/2008 4:37 PM Very useful.  Thanks for the tip!
SuperJason
SuperJason United States on 9/11/2008 4:49 PM Awesome, I had no idea.
Dave Transom
Dave Transom New Zealand on 9/11/2008 10:29 PM This is definitely the way to. Once you make the switch, you'll wonder why you didn't do it earlier Smile

Something else I found really helpful is setting the SMTP folder to a relative path (something you can't do in the web.config), so I use a bit of reflection to set mine to a project specific one - real handy for multiple developers with different project locations too.

Setting the pickup directory location at runtime:
www.singular.co.nz/.../...location-at-runtime.aspx
Ryan Farley
Ryan Farley United States on 9/12/2008 6:50 PM That is just awesome. Glad you posted this, I didn't know you could do that with just a config file.
CodeDigest.Com
CodeDigest.Com India on 9/13/2008 3:08 AM Excellent one!!Thanks!
Dave Schinkel
Dave Schinkel United States on 10/7/2008 7:45 PM Good stuff, especially the Vista issue.

www.codezest.com
Vladimir Dedek
Vladimir Dedek Czech Republic on 10/12/2008 4:35 AM Thanks for nice tip!
trackback
Code Junkie on 10/29/2008 7:49 PM Trackback from Code Junkie

Sending Mail To Your File System (No Need For A Mail Server)
pingback
code-inside.de on 3/16/2009 10:11 PM Pingback from code-inside.de

HowTo: Senden von Emails testen ohne Mailserver | Code-Inside Blog
pingback
blog.dotnet-oldenburg.de on 3/18/2009 11:18 AM Pingback from blog.dotnet-oldenburg.de

.NET Oldenburg Blog » E-Mail ohne Mailserver in ASP.NET testen
Chuck Caldwell
Chuck Caldwell United States on 4/25/2009 7:19 AM Thanks so much. This is perfect.
trackback
Hock Blogs .NET on 4/29/2009 8:48 AM Trackback from Hock Blogs .NET

Handy web.config snippet for sending email in a development environment
email2
email2 United States on 6/29/2009 2:15 PM Experts in all industries recommend finding a value oriented provider, one that will bring peace of mind and security system
SEO
SEO United States on 6/30/2009 2:28 AM Thanks. I've definitely not got that problem, and in fact I can get this working using System.Web.Mail. Microsoft tells me that I should be using System.Net.Mail instead, but it doesn't work!!
Search engine optimization
Search engine optimization United States on 7/1/2009 12:58 AM Hi,
  It's not about the tool when using twitter it's about the people. I would find it handy to have a system that is easy to implement with Lamson. So it would let me read and search tweets from a platform with no native twitter client like my cell phone (which does have email)..

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading