Configure SMTP Server on Windows 2012 to use Gmail to relay messages

If you need to quickly test your applications’ ability to send mail, you can setup Gmail as a smart host using your Gmail account for authentication.

Installing SMTP Server

Follow the steps 1 – 9 in this post for the manual installation or run the PowerShell commands below on your server:

PS C:\Users\admin> Add-WindowsFeature SMTP-Server,Web-Lgcy-Scripting
PS C:\Users\admin> Set-Service SMTPSVC -startuptype "automatic"
PS C:\Users\admin> Start-Service SMTPSVC

Configure SMTP Server

To configure the SMTP server to forward emails to Gmail, follow these steps:

1. Open Internet Information Services (IIS) Manager 6; click Start > Run and then enter inetmgr6 in the dialogue box then click OK to load IIS Manager 6.

2. In IIS 6 Manager right-click on SMTP Server and select Properties:

3. In the General tab, unless you want the SMTP Server to use a specific IP address,  leave the settings as they are so that the IP address is set to (All Unassigned):

4. To proceed, click on the Access tab:

5. Click on the Authentication button and ensure Anonymous access is checked and then click OK:

6. Once back in the Access tab, click on the Connection button. Select Only the list below and then click Add. Assuming that your application runs locally on the server, enter 127.0.0.1 as the IP address and then click OK:

The Connection setting controls which computers can connect to the SMTP server and send mail. By granting only localhost (127.0.0.1) access, limits only the server itself the ability to connect to the SMTP server. If the server running your application runs on a separate server to the SMTP server then add its IP address to the Connection list also.

7. Click OK to return to the Access tab and then click on the Relay button. Enter 127.0.0.1 as the IP address and then click OK:

The Relay section determines which computers can relay mail through this SMTP server. By only allowing the localhost IP address (127.0.0.1) relay permissions it means that only the server itself can relay mail. If the server running your application runs on a separate server to the SMTP server then add its IP address to the Connection list also.

8. Next, go to the Messages tab. Here you can enter an email address where copies of non-delivery reports are sent to. Leave the other default settings as they are:

20130427131034

9. Next, go to the Delivery tab:

10. Click on the Outbound Security button and ensure Basic authentication is selected, then enter your Gmail user name and password and check TLS encryption:

20140119212758

11. Click OK to return to the Delivery tab and then click on Outbound Connections. Leave the defaults as they are but change TCP port to 587:

20140119212904

12. Click OK to return to the Delivery tab and then click on the Advanced button:

20140120234701

Here you will need to enter the Fully-qualified domain name (FQDN) of the SMTP server. Successful mail delivery doesn’t depend on the FQDN being set in this instance as long as you have configured the correct Gmail credentials in step 19 and have set the Smart host.

Enter smtp.gmail.com in the Smart host section.  This is where the SMTP server will forward messages to. Gmail will then take care of actually delivering the mail to is intended recipient(s) if it successfully authenticates the connection using the Gmail user name and password provided in step 19.

Due to changes in Gmail’s security policy, the steps above are not enough to guarantee successful delivery through their mail platform. You also need to perform the steps below before proceeding to testing:

13. Login to your Gmail account and go this link, which contains your account information and security settings:

20170530125045

14. Then scroll down to the bottom of the Sign-in and security section and drag the Allow less secure apps to the ON position, per the below:

20170530125141

 

Testing

To test the Gmail smart host run the PowerShell command below, changing where appropriate the -SMTPServer-To and -From parameters:

PS C:\Users\admin> Send-MailMessage -SMTPServer localhost -To xxxxx@yourdomain.com -From xxxxx@gmail.com -Subject "This is a test email" -Body "Hi, this is a test email sent using Gmail as a smart host"

This email was delivered successfully to my Gmail inbox:

20140121202111

Note: The received message will always appear as if it were sent from your Gmail account, irrespective of the -From parameter in the PowerShell command above or in the message generated by your application.