Powershell: Manage IIS SMTP server

Setting up Microsoft Internet Information Services (IIS) SMTP service is pretty straight forward for simple implementations. It hasn’t really keept up with time and I’m pretty sure not to many people use it anymore. Working with an older implementation in a system that used distributed SMTP on each and every IIS server I realized we needed to centralize it so we could secure it properly. This included re configuring an old IIS SMTP server and then add a bunch of aliases to make sure the server accepted all the incoming e-mails.

Configuring IIS SMTP

To reconfigure the SMTP we can use Active Directory Service Interface from Powershell. This allows us to change settings we can’t from the GUI like pickup and badmail directories. Here is a simple code snippet how to handle configuration:

[ps]
$SMTPServer =[adsi]("IIS://localhost/SMTPSVC/1")
$SMTPServer.BadMailDirectory = "D:mailrootBadmail"
$SMTPServer.DropDirectory = "D:mailrootDrop"
$SMTPServer.PickupDirectory = "D:mailrootPickup"
$SMTPServer.QueueDirectory = "D:mailrootQueue"
$SMTPServer.CommitChanges()
[/ps]

Adding SMTP Aliases

By adding domain aliases you will force the SMTP service to accept e-mails for additional domains. From what I found online there was a lot of Powershell snippets for adding complete new domains but nothing handling the aliases. Here is the code:

[ps]
function AddSMTPAlias {
Param([Parameter(Mandatory=$true)]$DomainName,[Parameter(Mandatory=$true)]$SMTPvsNbr)

$SMTPDomain =[adsi]("IIS://localhost/SMTPSVC/" + $SMTPvsNbr + "/Domain")
$NewEntry = New-Object System.DirectoryServices.DirectoryEntry($SMTPDomain.Path)
$NewEntryCollection = $NewEntry.Children
$NewEntryToAdd = $NewEntryCollection.Add($DomainName, $NewEntry.SchemaClassName)
$NewEntryToAdd.RouteAction = 16
$NewEntryToAdd.CommitChanges()
}

$domains = Get-Content ‘c:tempdomains.txt’

foreach($domain in $domains)
{
AddSMTPAlias -DomainName $domain -SMTPvsNbr 1
}
[/ps]

The function takes two parameters, first up is the DomainName which is pretty self explanatory. The second one is SMTPvsNbr which is the number of the SMTP Service in the IIS. You can find this in the GUI and when you list all the SMTP Services via Powershell. The secrete source here is the RouteAction equals 16 which means domain alias.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: