Ever wondered how to programatically configure SMTP service on IIS to route mails to a particular domain?

IIS settings are maintained in a Metabase file. We can programatically edit this metafile to add settings that will route emails to any domain (say mail@foo.com).

Lets say, we want to input information to IIS on how to route mails to foo.com. For that we need to add a new remote routing domain information in IIS. To do this via the user interface refer link. IIS Metabase is in a structured format and we will directly access this structure to configure  routing for domain foo.com. More information on IIS Metabase structure can be found at here.

Here is the code that will add a new routing domain.

' Create an instance of the SmtpServer object
' that represents the default Web site.
Set IIsSmtpServer = GetObject("IIS://localhost/smtpsvc/1")

' Use the Windows ADSI container object "Create" method to create
' a new SmtpDomain.

Set NewDomain = IIsSmtpServer.Create("IIsSmtpDomain", "Domain/nclmailtest.com")

' set some required properties.
NewDomain.RouteAction = 2
NewDomain.RouteActionString = "MyMailHost"

' Use the Windows ADSI object "SetInfo" method to
' save the data to the metabase.
NewDomain.SetInfo

Explanation:

By Default, if a SMTP server is configured it can be accessed by accessing IisSmtpServer object with Id smtpsvc/1. If more than one Smtp server is configured (Win2k3) then each IisSmtpServer object can be accessed by IIS://localhost/smtpsvc/n where n is the number of Smtp Virtual server you want to access.

Set IIsSmtpServer = GetObject("IIS://localhost/smtpsvc/1")

Next we want to add a Routing domain to this Virtual Server so we create a new IisSmtpDomain object for this Server. Provide a unique name for this domain configuration in Domain\[UniqueName].

Set NewDomain = IIsSmtpServer.Create("IIsSmtpDomain", "Domain/foo.com")

This is a remote routing domain configuration and so we need to set the RouteAction flag to denote the same. RouteAction flag for a remote domain configuration is 2.

NewDomain.RouteAction = 2

If your local DNS does not know how to resolve foo.com then we need to specify the smart host that will handle mails by specifying the host name in RouteActionString property. If you dont want to set the smart host property then just dont set the RouteActionString field on this object.

NewDomain.RouteActionString = "MyMailHost"

Considering the host allows Anonymous forwarding of mails, we dont have to set any other properties. All that is left to be done is store this new object in the metabase file and then reset IIS for the changes to take effect.

To reset IIS programatically, execute command 

iisreset /restart

If a Alias is to be configured for this Server then the RouteAction field value for that is 16

Also, default properties for the Smtp Virtual Server such as DropFolderLocation, pickup folder location, bad mail folder location can be changed by setting/getting the appropiate fields on IisSmtpServer object. Details on the Smtp objects can be found here. For example, to get the BadMailFolder directory of the default virtual Server, call (This is not available on IIS5.0 and earlier versions)

WScript.Echo "Bad mail folder location is " & IIsSmtpServer.BadMailDirectory

This posting is provided "AS IS" with no warranties, and confers no rights