Monday, November 14, 2011

asp.net smtp email configuration for localhost testing

I had a opportunity to work on a project where the client wanted to send email to his clients once the data is inserted in the database.
Once I started working on that portion of the code, the client still didn't had the smtp settings he'll use for his website online, so I needed to test my code in local envionment and make sure that everything works fine.
First thing I did was to add a system.net node in the web.config file and configure that accordingly to the localhost envionment:

<system.net>
<mailsettings>
<smtp from="arnaudovangel@yahoo.com" deliverymethod="SpecifiedPickupDirectory">
<specifiedpickupdirectory pickupdirectorylocation="C:\TempWebService\">
<network host="localhost">
</network></specifiedpickupdirectory></smtp>
</mailsettings>
</system.net>

Once I had that done, I created a method to send the email, with a parameter for the outgoing email address. The method was looking something like this:

public void sendEmail(string emailAddress)
{
MailMessage msg = new MailMessage("test@test.com", emailAddress, "test", "test body");
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Send(msg);
}
Once that method was executed, a new mail messsage was created in the folder I specified, in this case: C:\TempWebService.
Once you apply the real smtp settings, the message will be delivered to the selected email.

Thanks for reading, Angel