IIS SMTP Email Sender

Naduni Pamudika
Aug 8, 2017 · 4 min read

All of the providers like Gmail APIs (2000 mails per day), SendPulse (12000 mails per month), and third party tools providing SMTP service like MailGun (10000 mails per month) have their own limitations. But IIS SMTP server can be considered as unlimited when compared to the aforementioned options. Because common email providers like Yahoo! Mail or Gmail do not allow clients to send unlimited emails at once: they have some strict limitations both for the number of messages and the number recipients you can handle per day.

So if you need to mail out a newsletter to a large number of accounts, you need to use a professional SMTP service, that is, a monitored outgoing server that allows sending unlimited emails with no concerns, maximizing your delivery rate. Here I explain how to setup an SMTP server to send auto-generated emails.


Setup & Configure SMTP Server in Windows Server 2012

When using Windows Server 2012 the SMTP feature must be installed and configured first. Below are the steps for installing the internal Windows SMTP Service.

Install the SMTP Server:

  1. Launch the Server Manager.
  2. From the Dashboard, click on Add Roles and Features to begin the wizard. Click Next on the first screen.
  3. Select ‘Role-based or feature-based installation’.
  4. Select the Server.
  5. Click Next to bypass the Roles selections.
  6. Scroll down the list and tick the ‘SMTP Server’ feature.
  7. A new window will pop up to inform you that some other services will also be installed. Click Add Features to confirm and continue.
  8. Click Next to continue past the features selection screen.
  9. Click Install to complete the installation.
  10. Click Close when the installation has completed.

Configure the SMTP Service:

  1. Launch the Internet Information Services (IIS) 6.0 Manager.
  2. Click Yes to the UAC Prompt.
  3. Right-click on SMTP Virtual Server #1 and select Properties.
  4. Select the General tab and change the IP Address to the server’s IP address.
  5. Select the Access tab and click on the Connection button.
  6. In the Connection window, select ‘Only the list below’ and click the Add button.
  7. Add the IP address, Group of computers or Domain of the devices permitted to use the internal SMTP, then click OK.
  8. In the Access tab of the Connection window, click the Relay button. Add the same permitted devices to the Relay Restrictions list.
  9. In the Delivery tab of the Connection window, set an external domain by clicking Advanced tab.
  10. Launch the Windows Firewall with Advanced Security console.
  11. Right-click on ‘Inbound Rule’ and select ‘New Rule’ to begin the wizard.
  12. Select ‘Port’ and click Next.
  13. Set the local port to 25 and click Next.
  14. Choose ‘Allow the connection’ and click Next.
  15. Uncheck ‘Public’ to prevent external access to the server. Click Next.
  16. Give the rule a name (Internal SMTP Service MDB), and click Finish.
  17. You should now see a new rule enabled in the firewall management console.
  18. In the Server Manager, click on Tools and select Services.
  19. In the Services window, double-click on ‘Simple Mail Transfer Protocol (SMTP)’.
  20. Stop and then Start the service. Set the Startup type to Automatic.
  21. Click OK to finish. The internal SMTP service is now ready and listening.

Send Emails using SMTP Server

  • Create a console application in Visual Studio 2015.
  • Create a SMTP Client object and set other parameters as below.
SmtpClient SmtpServer = new SmtpClient("OPD");
SmtpServer.Port = 25;
SmtpServer.EnableSsl = false;
SmtpServer.UseDefaultCredentials = false;
String userName = ConfigurationManager.AppSettings["UserName"];
String password = ConfigurationManager.AppSettings["Password"];
SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
  • Add configuration values to App.config file.
<appSettings>
<add key="UserName" value="User"/>
<add key="Password" value="******"/>
</appSettings>
  • Create the “MailSender” class to create the MailMessage object with all details and send the mail using the created SMTP client object above.
class MailSender {
public SmtpClient SmtpServer;
public MailSender(SmtpClient SmtpServer) {
this.SmtpServer = SmtpServer;
}
private MailMessage CreateMail() {
using(StreamReader reader = File.OpenText("~/MailTemplate.html")) {
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc@gmail.com");
mail.To.Add("testsmtpmail16@gmail.com");
mail.Subject = "Test SMTP Mail";
mail.IsBodyHtml = true;
mail.Body = reader.ReadToEnd();
mail.Body = mail.Body.Replace("{UserName}", "YourName");
mail.Body = mail.Body.Replace("{Link}", "http://any-link");
return mail;
}
}
public void SendMails() {
MailMessage mail = CreateMail();
for (int i = 0; i < numOfMails; i++) {
SmtpServer.Send(mail);
Console.WriteLine("Message Sent " + i);
}
}
}
  • Create a new thread and send mails using it, so that it will not affect the main flow of the application.
MailSender mailSender = new MailSender(SmtpServer);
Thread mailThread = new
Thread(new ThreadStart(mailSender.SendMails));
mailThread.Start();
  • Here for the usability of application, separate html file has been used.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Email</title>
</head>
<body bgcolor="#ebebeb">
<table cellspacing="0" cellpadding="0" width="100%" bgcolor="#ebebeb">
<tbody>
<tr>
<td align="center" valign="top" style="font-size:20px;font-weight:700">Welcome to SMTP Mail Service</td>
</tr>
<tr>
<td align="justify">Hello <b>{UserName}</b>,</td>
</tr>
<tr>
<td align="justify">
<br />
This is a test mail to test SMTP Mail Service.
<br />
We use IIS <b>SMTP server</b>, because common email providers like Yahoo! Mail or Gmail do not allow clients to send unlimited emails at once: They have some strict limitations both for the number of messages and the number recipients you can handle per day.
<br />
Enjoy using SMTP Mail Service :)
<br />
<br />
Thank You!
</td>
</tr>
<tr>
<td>For more details <a href="{Link}"><button>Click Here</button></a></td>
</tr>
</tbody>
</table>
</body>
</html>
  • Run the application and you will see the received mails in receivers’ inbox.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade