Friday, November 24, 2006

How to configure the IIS SMTP Service to send emails from ASP.NET code?

I have probably configured the IIS SMTP service a dozen times over the past five years but I still struggle with authentication and relay, so I have decided to write this note once and for all.

First, the source for your System.Net.Mail issues is definitely http://www.systemnetmail.com/, including some sample code.

Because we are all urged to deliver rapidly, I have provided a test application which you can download from http://www.velodoc.com/downloads/061124.blog.zip. Contrary to most samples that you will find on the Internet, this one uses a configuration file including system.net\mailSettings, which is probably the way you want to send emails from your ASP.NET code.

Obviously you need to install the SMTP Service, which is well explained at http://msdn2.microsoft.com/en-us/library/8b83ac7t.aspx.

Let’s say you need to send your emails from website@yourdomain.tld. In this case, it is recommended (but not required) that you rename the default domain into yourdomain.tld.

Display the default SMTP Virtual Server properties and on the Access tab:

  1. Configure authentication by removing Anonymous access and allowing Basic authentication and/or integrated Windows authentication;
  2. Grant relay to 127.0.0.1 and allow all computers which successfully authenticate to relay.


You can certainly be more restrictive depending on your requirements, once you got teh above configuration to work properly.

Finally, you can create a dedicated account and properly configure your settings:

<system.net><mailSettings>
<smtp deliveryMethod="Network" from="website@yourdomain.tld">
<network host="127.0.0.1" port="25" userName="DOMAIN\account" password="password"/>
</smtp>
</mailSettings>
</system.net>

No comments: