Friday, May 6, 2011

Testing Emails in ASP.NET without a mail server

Many of times it happens that we want to checkout whether the mail function is working properly without configuring the SMTP server..
Here is a simple trick to although to fullfil your need.
Follow the steps given below:
Step 1. Open the configuration file web.confing located at the root of your web application.
Step 2. Add the following stuff in it.

<system.net>
<
mailSettings>
<
smtp deliveryMethod="SpecifiedPickupDirectory">
<
specifiedPickupDirectory pickupDirectoryLocation="C:\emails\"/>
smtp>
mailSettings>
system.net>
things to note in the above code is specifiedPickupDirectory element which performs the task of configuring the local directory for SMTP. Second thing is pickupDirectoryLocation. This is the location on your local drive where your application keep the mail to be used by SMTP later.

So what's more remaining is testing the above settings.
write the following code in the click event of your send mail button's click event.

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("fromThis@email.com",
"toThis@email.com", "subjecthere", "Testing email without smtp configuration");
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Send(mail);

No comments:

Post a Comment