Wednesday, September 12, 2012

Test email functionality in ASP.NET application using "Test Mail Server Tool""


At times we need to test the email functionality in our projects, during development and testing phases, and for that we prefer actual email sending does not take place. 

There are many tools available over internet, I prefer using "Test Mail Server" utility.

You can download it from here.

This article explains the steps to test the email functionality using this utility.

Following is the code extract and steps to check if Test Mail Server tool is working or not.

1) Copy following code in a code-behind (.cs) of your ASPX page in you website application.


System.Net.Mail.SmtpClient _smtpClient = new System.Net.Mail.SmtpClient("localhost", 25);
System.Net.Mail.MailMessage _sampleMail = new System.Net.Mail.MailMessage("sender@gmail.com", "receiver@gmail.com", "Test email - This is working", "This is a test email to check if Send Test Mail utility is working!");
_smtpClient.Send(_sampleMail);

Also make sure, you configure SMTP settings in web.config file, rather than hard-coding it in compiled code, and by doing that we can avoid supplying "localhost" and port number in source code. Its not done in this walkthrough  just to minimize the steps.


2) Make sure you have "Test Mail Server Tool" running on your machine.
3) Also, confirm the port number, and mail storage directory:
(Right-click on running instance of this utility in system tray, and click "Options")

If you want to change the port number, make sure you also change it in the code. (See, the yellow highlighted in Step 1)
4) Run the website, and execute send test mail code.
P.S. Sometimes it takes a while (say, 3-4 minutes) to generate a test email, save and open it. but it will always happen in background by this utility. If this happens, you can still close the browser after email send code is executed, and email will be shown to you once generated.

No comments:

Post a Comment

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman