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("[email protected]", "[email protected]"); 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:
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: [email protected] 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.
Leave a Reply
You must be logged in to post a comment.