Posted by: Cirilo Meggiolaro | 04/2/2009

Tip of the day #170 – Testing e-mail message formats without smtp server

Let’s imagine the following scenario: You are developing a feature that creates e-mail messages and you want to quickly test if the e-mail format is ok. To achieve that you may specify a directory where the messages will be saved so instead of trying to access the smtp server you are going to save the file that you can view in Outlook.

How to…

  • On your application’s configuration file add a smtp section and set the deliveryMethod attribute to “SpecifiedPickupDirectory”;
  • Specify a directory to save the messages by adding a specifiedPickupDirectory element and setting the pickupDirectoryLocation attribute to a path to save the messages;
  • The configuration section must be similar to the following:

<system.net>
    <mailSettings>
        <smtp deliveryMethod=SpecifiedPickupDirectory>
            <specifiedPickupDirectory pickupDirectoryLocation=C:\MyMessages />
        </smtp>
    </mailSettings>
</system.net>

If you want to make a quick test, create a Message and a SmtpClient object. Both are available under the System.Net.Mail namespace. The code will be similar to the following:

MailMessage msg = new MailMessage(“myemail@hotmail.com”, “myemail@gmail.com”);
msg.Body = “This is the message body.”;
msg.Subject = “This is the message subject.”;

SmtpClient smtp = new SmtpClient(string.Empty); //Empty just for testing purposes.
smtp.Send(msg);

Check the directory you specified in the configuration file. An eml file with a guid name will be there. Just double-click on it to view the content on the Outlook.

Advertisements

Responses

  1. Never saw that before. Is that from 3.5 or we already had that in .NET 2.0?

  2. Yes. It was included in .NET 2.0.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: