Configuring SMTP Server Using PowerShell

Let us first go through the manual steps required for configuring SMTP server and PowerShell script is included in the end.

For this example, we are using Microsoft Windows Server 2016 and the configuration might change based on the operating system.

Restart the server and test the smtp mail functionality using below PowerShell command

Send-MailMessage -SMTPServer localhost -To to@gmail.com -From from@gmail.com -Subject “Test” -Body “Success”

Note: Sometimes we must restart the server, smtp service multiple times and also leave the server ideal for about 2 hours for the SMTP server to send emails.

Below is the PowerShell script for automating above process

#Installing SMTP server—————————————————————————-

$smtp = get-WindowsFeature “smtp-server”
if(!$smtp.Installed){
Add-WindowsFeature $smtp -IncludeAllSubFeatur
}

#Installing Telnet————————————————————————————–

$telnet = get-WindowsFeature “telnet-client”
if(!$telnet.Installed){
Add-WindowsFeature $telnet -IncludeAllSubFeature
}

#Setting Relays—————————————————————————————–

$IpRelayList = @(“127.0.0.1”)

$iisObject = new-object System.DirectoryServices.DirectoryEntry(“IIS://localhost/smtpsvc/1”)
$relays = $iisObject.Properties[“RelayIpList”].Value

#update

$bindingFlags = [Reflection.BindingFlags] “Public, Instance, SetProperty”
$relays.GetType().InvokeMember(“IPGrant”, $bindingFlags, $null, $relays, $IpRelayList);

$iisObject.Properties[“RelayIpList”].Value = $relays
$iisObject.CommitChanges()

#Setting ConnectionIps—————————————————————————–

$iisObject = new-object System.DirectoryServices.DirectoryEntry(“IIS://localhost/SmtpSvc/1”)
$ipSec = $iisObject.Properties[“IPSecurity”].Value

[Object[]] $grantByDefault = @()
$grantByDefault += , $false

$ipSec.GetType().InvokeMember(“GrantByDefault”, $bindingFlags, $null, $ipSec, $grantByDefault);

$iisObject.Properties[“IPSecurity”].Value = $ipSec
$iisObject.CommitChanges()

$connectionips = “127.0.0.1”

$iisObject = new-object System.DirectoryServices.DirectoryEntry(“IIS://localhost/SmtpSvc/1”)
$ipSec = $iisObject.Properties[“IPSecurity”].Value

#update

$bindingFlags = [Reflection.BindingFlags] “Public, Instance, SetProperty”
$ipSec.GetType().InvokeMember(“IPGrant”, $bindingFlags, $null, $ipSec, $connectionips);

$iisObject.Properties[“IPSecurity”].Value = $ipSec
$iisObject.CommitChanges()

#Setting Host, UserName & Password——————————————————–

$iisObject = new-object System.DirectoryServices.DirectoryEntry(“IIS://localhost/SmtpSvc/1”)

$iisObject.Properties[“RouteAction”].Value = 268
$iisObject.Properties[“RouteUserName”].Value = “UserName”
$iisObject.Properties[“RoutePassword”].Value = “Password”

$iisObject.Properties[“SmartHost”].Value = “smtp.gmail.com”

$iisObject.CommitChanges()

#Resart Service—————————————————————————————–

Set-Service -Name “SMTPSVC” -StartupType Automatic
Restart-Service “SMTPSVC”

#Test Mail————————————————————————————————-

Send-MailMessage -SMTPServer localhost -To to@gmail.com -From from@gmail.com -Subject “Test” -Body “Success”

Note: Update your Gmail username, password in the script.

Happy Coding 🙂

Please leave a comment below, and let me know what you think!

About the Author

Kishore Ithadi

https://in.linkedin.com/in/kishoreithadi

Leave a Reply

Your email address will not be published. Required fields are marked *