Using the Windows SMTP Server in Azure

If you want to use the built in SMTP server in an Azure role then there are a few hoops you need to jump through. Firstly, you need a reliable way to install it each time a role instance comes up. This is done with a startup task in your ServiceConfiguration.csdef file:

<Startup>
  <Task commandLine="Startup.cmd" executionContext="elevated" taskType="simple" />
</Startup>

The Startup.cmd needs to do 2 things. First, it uses powershell to install the SMTP server and secondly, it uses a vbscript file to configure the SMTP server once installed. Let’s look at the powershell code first. You need to tell the powershell environment to allow unsigned scripts before running the installation script.

powershell -command "Set-ExecutionPolicy Unrestricted"
powershell .\InstallSmtpServer.ps1

The InstallSmtpServer.ps1 contains the following:

Import-Module Servermanager
Add-WindowsFeature SMTP-Server

The first line imports the module for configuring the server and the second line add the SMTP server. This can take a couple of minutes to run. Having installed the SMTP server, we now need to configure it. There are 2 things we need to do here. By default the SMTP server will not allow any relaying, so if you want to send email from this server then you have to add 127.0.0.1 to the grant list. Next, you have to add one or more alias domains if you want to receive emails. There are several ways to do this, but the simplest is to use good old VB script and the ADSI objects from the good ol’ days. Here is the script:

Option Explicit
Dim iisSmtpServer, relayIpList, domainObject, newDomain,alias

alias = "myapp.cloudapp.net"
' Create an instance of the SmtpServer object that represents the default smtp server.
Set iisSmtpServer = GetObject("IIS://localhost/smtpsvc/1")
' Get the current relay ip list
Set relayIpList = iisSmtpServer.Get("RelayIpList")
' Set to allow only specified ip addresses
relayIpList.GrantByDefault = false
relayIpList.IpGrant = "127.0.0.1"
' Save the settings
iisSmtpServer.Put "RelayIpList",RelayIpList
iisSmtpServer.SetInfo
' Get the smtp server domain object
Set domainObject = GetObject("IIS://LocalHost/SMTPSVC/1/domain")
' Create the aliases
Set newDomain = domainObject.Create ("IIsSmtpDomain",alias)

' Set the route action to be an ALIAS domain
newDomain.RouteAction = 16
newDomain.RouteActionString = alias
' Save the settings
newDomain.SetInfo

Run the script from your startup.cmd like this:

cscript setupsmtpserver.vbs

You can modify this script to alter all of the SMTP server features to suit your needs.

Having done all this, don’t forget to add port 25 to your Endpoints list in csdef e.g.

 <InputEndpoint name="smtp" port="25" protocol="tcp" localPort="25" />

So that’s it. We now have a dynamically installed SMTP server in Azure. As a word of caution, be aware that if you send large quantities of email from Azure, you may well run the risk of having the IP address blocked by anti-spam systems.

Happy emailing 😮

Advertisements