Dynamically install an SMTP server in Windows Azure

1) Set the “osFamily” to “2” in the “ServiceConfiguration” tag In the “ServiceConfiguration.cscfg” file, it defines the OS version in Azure:

 1 – Specifies the OS family that is substantially compatible with Windows Server 2008 SP2.

2 – Specifies the OS family that is substantially compatible with Windows Server 2008 R2.

         Of these two, Windows Server 2008 R2 supports Powershell 2.0, which is necessary for smtp configuration.

        (More details in reference 3)

2) Download the AzureSMTP.zip file and copy the files to your portal solution. Make sure all these files get copied to the output directory. You can make changes in the setupsmtpserver.vbs file to make it fit for your application:

 e.g.: change the alias = “yoursitename.cloudapp.net”

InstallSMTP.cmd            ->  Batch file for installing and configuring the smtp service

InstallSMTPServer.ps1   ->  Powershell batch file for adding the smtp server module in windows 2008

Setupsmtpserver.vbs   ->  VB script file for configuring the smtp server

(More Details in reference 1)

3) In “ServiceDfinition.csdef”

a)      Add the following task to the startup block

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

b)  Add port 25 to Endpoints as following

<Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
      <InputEndpoint name="smtp" port="25" protocol="tcp" localPort="25" />
</Endpoints>

 

4)  In “web.config”, set the smtp address to 127.0.0.1 as shown below.

<mailSettings>
      <smtp from="webmaster@mydomain.com">
        <network host="127.0.0.1" />
      </smtp>
</mailSettings>

Then, you are ready to go and deploy the site. You will see a smtp Virtual Server running in IIS 6 from RDP and you will be able to send/receive emails in Azure.

Notes:

1. Since you have added one more InputEndpoint in this deployment, you cannot upgrade from old deployments in Azure. The deployment time will be little longer than usual.

2. Keep the sender’s email domain consistent with your app domain to avoid being recognized as junk mail in Hotmail etc.

3. “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.”

References :

  1. Using the Windows SMTP server in Azure: http://richardprodger.wordpress.com/2011/07/18/using-the-windows-smtp-server-in-azure/
  2. Installing Windows Features in a Windows Azure Role Instance: http://blogs.msdn.com/b/golive/archive/2011/08/15/installing-windows-features-in-a-windows-azure-role-instance.aspx
  3. Windows Azure Service Configuration Schema: http://msdn.microsoft.com/en-us/library/ee758710.aspx#ServiceConfiguration
  4. Running PowerShell Scripts in Windows Azure Start-up Tasks: http://blog.syntaxc4.net/post/2011/02/08/Running-PowerShell-Scripts-in-Windows-Azure-Start-up-Tasks.aspx