Tuesday, October 25, 2011

Sending e-mails using IIS SMTP Server on Windows Azure

If you want to send emails from a Windows Azure Role, there’s the possibility to use the built-in SMTP Server of IIS 6.0. Here is a guide how to use startup scripts and PowerShell commands to set up a SMTP server in the cloud.

Before you start make sure that you’re running a Windows 2008 Server R2 in the cloud by setting the osFamily Attribute to osFamily=2 in the ServiceConfiguration.cscfg file.

Setup the SMTP

Here is a guide that shows how to set up the SMTP server manually. Those are the same steps we’re going to do here using scripts. It helped me to understand what’s necessary to do. Just deploy any Azure Project to the Cloud, RDP in, install the SMTP-Server feature and do the steps described in that guide.

But now to the automated setup scripts:

1. Create a new startup task in the ServiceDefinition.csdef

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


2. The startup file first needs to install the SMTP-Server feature automatically. To gain this it uses the PowerShell 2.0. After that it calls a VBScript that configures the SMTP-Server.



The Startup.cmd:

powershell -command "Set-ExecutionPolicy Unrestricted"

powershell .\InstallSmtp.ps1

cscript ConfigSmtp.vbs



The InstallSmtp.ps1 PowerShell script:

Import-Module Servermanager
Add-WindowsFeature SMTP-Server


3. Now we need to create the VBScript that configures the SMTP server: We need to add all IP addresses that are allowed to send mails through this SMTP server to the grant list. Assuming only applications running on the same server are allowed to send emails we’re going to add 127.0.0.1 to that list.



The ConfigSmtp.vbs

Option Explicit
Dim smtpServer, relayIpList
' Get the default instance of the SMTP server
Set smtpServer = GetObject("IIS://localhost/smtpsvc/1")
' Get the IPList
Set relayIpList = smtpServer.Get("RelayIpList")
' Add localhost to that list
relayIpList.GrantByDefault = false
relayIpList.IpGrant = "127.0.0.1"
' Save changes
smtpServer.Put "RelayIpList",relayIpList
smtpServer.SetInfo


4. Deploy to the cloud



Use the SMTP from C# code



To use the local SMTP server from within your C# code use the following lines:

var client = new System.Net.Mail.SmtpClient("localhost");
client.Send("from@domain.tld",
            "to@domain.tld",
            "This is my subject",
            "Hello, this is a mail from the cloud!");


Don’t get blacklisted



Even Steve Marx recommended not to use the SMTP server feature on Windows Azure instances because they soon would be blacklisted. To avoid getting on a blacklist you could use a smart host to deliver your emails with.



If you want to use a smart host in your deployment, you need to extend the ConfigSmtp.vbs:

' set the outbound connector to a smart host
smtpServer.SmartHostType = 2
smtpServer.SmartHost = "smtp.mysmarthost.tld"
' use basic authentication
smtpServer.RouteAction = 264
smtpServer.RouteUserName = "myName"
smtpServer.RoutePassword = "myPassword"
' save changes
smtpServer.SetInfo


In this case I have been using the SMTP relay services offered by http://dyn.com which worked just fine. Depending on what service you’re about to use, the settings might differ.





References



6 comments:

  1. Hi Nico, just a note: in the ConfigSmtp.vbs, you must set the RelayIpList with this command before calling the .SetInfo method

    smtpServer.Put "RelayIpList",relayIpList

    Good article!

    ReplyDelete
    Replies
    1. Hey David,

      thank you for that hint. Seems as if I forgot to paste that line into the script in this blog :)

      Nico

      Delete
  2. I think you mean .csdef in two places, not .cscfg.

    Also, are "//" really valid comments in vbs files?

    ReplyDelete
    Replies
    1. Hey Per,

      you're right on both points. Thank you! I should think about doing a new post on this topic.

      Delete
    2. Fixed it meanwhile within this post...

      Delete
  3. wow this good but ,I like your post and good pics may be any peoples not like because defrent mind all poeple , startup programs

    ReplyDelete