Configure the IIS 6 SMTP server with WMI and PowerShell

Recently one of my customers asked me how to configure IIS 6 SMTP domains via PowerShell during an unattended SharePoint farm installation. I was not able to find a complete example, so I thought this would be a great topic for my first blog post.

The PowerShell script will create a new incoming SMTP alias domain and then configure selected properties like max. message size, etc. It uses WMI to create the domain and configure the properties.

This should work on all Windows versions that support IIS 6 and PowerShell, but I have only tested it on Server 2008 R2.

Enjoy!

 

Set-ExecutionPolicy RemoteSigned

function Configure-SMTPService ([string]$incomingEMailDomainName, [int]$incomingEMailMaxMessageSize)

{

       Write-Host -Foregroundcolor White " -> Changing the start-up type of SMTP service to 'Automatic'..."

       Set-Service "SMTPSVC" -StartupType Automatic -ErrorAction SilentlyContinue

       if ($?)

       {

             Write-Host -Foregroundcolor Green " [OK] Successfully changed startup type."

       }

       else

       {

             Write-Host -Foregroundcolor Red " [Error] Unable to change startup type."

             Exit

       }

      

       Write-Host -Foregroundcolor White " -> Starting SMTP service..."

       Start-Service "SMTPSVC" -ErrorAction SilentlyContinue

      

       if ($?)

       {

             Write-Host -Foregroundcolor Green " [OK] Service successfully started."

       }

       else

       {

             Write-Host -Foregroundcolor Red " [Error] Unable to start service."

             Exit

       }

      

       # Ascriptomatic is a great tool to explorefor exploring WMI namespace is scriptomatic:

       # https://www.microsoft.com/en-us/download/details.aspx?id=12028

      

       Write-Host -Foregroundcolor White " -> CreatingCreate incoming SMTP domain..."

      

       # First create a new smtp domain. The path 'SmtpSvc/1' is the first virtual SMTP server. If you need to modify another virtual SMTP server

       # change the path accordingly.

       try

       {

             $smtpDomains = [wmiclass]'root\MicrosoftIISv2:IIsSmtpDomain'

             $newSMTPDomain = $smtpDomains.CreateInstance()

             $newSMTPDomain.Name = "SmtpSvc/1/Domain/$incomingEMailDomainName"

             $newSMTPDomain.Put() | Out-Null

             Write-Host -Foregroundcolor Green " [OK] Successfully created incoming email domain."

       }

       catch

       {

             Write-Host -Foregroundcolor Red " [Error] Unable to create incoming email domain."

             Exit

       }

      

       Write-Host -Foregroundcolor White " -> Configuring incoming SMTP domain..."

      

       try

       {

             # Configure the new smtp domain as alias domain

             $smtpDomainSettings = [wmiclass]'root\MicrosoftIISv2:IIsSmtpDomainSetting'

             $newSMTPDomainSetting = $smtpDomainSettings.CreateInstance()

 

             # Set the type of the domain to "Alias"

             $newSMTPDomainSetting.RouteAction = 16

 

             # Map the settings to the domain we created in the first step

             $newSMTPDomainSetting.Name = "SmtpSvc/1/Domain/$incomingEMailDomainName"

             $newSMTPDomainSetting.Put() | Out-Null

             Write-Host -Foregroundcolor Green " [OK] Successfully configured incoming email domain."

       }

       catch

       {

             Write-Host -Foregroundcolor Red " [Error] Unable to configure incoming e-mail domain."

             Exit

       }

       Write-Host -Foregroundcolor White " -> Configuring virtual SMTP server..."

       try

       {

             $virtualSMTPServer = Get-WmiObject IISSmtpServerSetting -namespace "ROOT\MicrosoftIISv2" | Where-Object { $_.name -like "SmtpSVC/1" }

            

             # Set maximum message size (in bytes)

             $virtualSMTPServer.MaxMessageSize = ($incomingEMailMaxMessageSize * 1024)

            

             # Disable session size limit

             $virtualSMTPServer.MaxSessionSize = 0

            

             # Set maximum number of recipients

             $virtualSMTPServer.MaxRecipients = 0

            

             # Set maximum messages per connection

             $virtualSMTPServer.MaxBatchedMessages = 0

             $virtualSMTPServer.Put() | Out-Null

             Write-Host -Foregroundcolor Green " [OK] Successfully configured virtual SMTP server."

       }

       catch

       {

             Write-Host -Foregroundcolor Red " [Error] Unable to configure virtual SMTP server."

             Exit

       }

}

Configure-SMTPService "sp.mydomain.local" 10240