How to test email functionality without configuring Smtp Client

Writing code for sending mail is one of the common tasks that we need to work every now and again. Sometimes it is one of the requirements in our application and some time we develop to send alerts to all when application looses its control on production :).

.NET made it very simple to send the mail which normally looks a complicated task. We can easily write 4-5 lines of the code to send the mail. Lets just have a look on a sample code

// Creating MailMessage object with From and To address
MailMessage message = new MailMessage("", "");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Test mail using SmtpClient";
message.Body = "Details of test mail using SmtpClient";

// Add more details to your message object

// Creating SmtpClient object
SmtpClient client = new SmtpClient("Provide smtp server details");
client.UseDefaultCredentials = true;
catch (Exception exception)
    // Log Exception

Above code snippet is just to send a simple mail (System.Net). But It also provides rich APIs which can be used to leverage the features like sending attachment, set body as html etc.

One of the key points in the code that it uses a smtp server to send the mail. That smtp server must be configured so that it can be used. Normally this kind of configuration we used to have on production or Pre-production environment. But what if we want to send from our local machine or integration server or test our functionality. It becomes very important because most of the time it has been found that look and feel of the body gets changed when it reaches in inbox or it looks way different when we open the email. So we need to see verify mail format then only we can assure about the functionality. Similarly there could be many other things to verify.

The Good news is that we have a very simple way which can be used to test the email functionality. We just need to add the following configuration on web.config.

      <!-- Setting delivery method as SpecifiedPickupDirectory-->
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <!-- Setting the folder path-->
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Data\TestMails\"/>

If we closely look the configuration, here we find that in smtp tag the deliveryMethod is set as  SpecifiedPickupDirectory then we added the specifiedPickupDirectory tag and provided the pickupDirectoryLocation  as the folder where we want to save the mails.Now the mails should be saved in provided folder. Lets run the code and see the folder

folderHere we can see that there is a file got saved in eml format. Lets open it

mailNow no matter whatever smtp details we provide in our code (see in the sample code, we just put the normal explanatory text), the mail would be saved in provided folder in configuration. SMTP Client checks the details from configuration if it is available then acts accordingly. if it is available save the file else try to contact the server given and if there is any issue throws an exception.

Hope this help you while testing.



Leave a Reply

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

You are commenting using your 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 )

Connecting to %s