the MMM blog

How to Send Email Using IIS localhost Virtual SMTP

How to Send Email Using IIS localhost Virtual SMTP


In order to send emails from IIS web server, you need to have the Simple Mail Transfer Protocol (SMTP) service installed and configured on your IIS server. Steps are:

1. Install IIS (IIS 6.0). Yes, it has to be IIS 6.0 because other version of IIS doesn't have virtual SMTP service. Installing IIS 6.0

2. Install Virtual SMTP Server on IIS 6.0

  1. On the Start menu, click Control Panel, double-click Add or Remove Programs, and then click Add/Remove Windows Components.

  2. In the Components list, click Application Server, and then click Details.

  3. In the Subcomponents of Application Server list, click Internet Information Services (IIS), and then click Details.

  4. In the Subcomponents of Internet Information Services (IIS) list, select the SMTP Service check box, and then click OK.

  5. Click Next. If you are prompted for the Windows CD or the network install path, follow the instructions.

  6. Click Finish.

3. Configure Virtual SMTP Server on IIS 6.0 Manager

  1. Open IIS 5.0 Manager, go to Internet Information Services -> your server -> SMTP Virtual Server

  2. right click on "SMTP Virtual Server" -> select "properties"

  3. go to "Access" tab

  4. in "Authentication", make sure "Anonymous access" is ticked.

  5. in "Connection", Add "127.0.0.1" to the list and check "Only the list below".

  6. in "Relay", Add "127.0.0.1" to the list and check "Only the list below". Tick "Allow all computers which successfully authenticate to relay,...

4. Add the email sending code in your application.

using (var client = new SmtpClient
{
    Host = "127.0.0.1",
    Port = 25
 })
 {
    var mail = new MailMessage();
    mail.From = new System.Net.Mail.MailAddress("youremail@yourdomain.com");
    mail.To.Add("youremail@yourdomain.com");
    mail.Subject = "Email Sent from localhost";
    mail.Body = "Your email body";
    client.Send(mail);
}

Note:  The SMTP service is "Manual" startup by default, so you may need to change it to "Automatic" if you would like the virtual SMTP server running all the time.

 

You don't need local SMTP service if you are using Gmail or Office 365 email server. You can apply the following code to simply make it work.

using Gmail

MailMessage msg = new MailMessage();
...
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "yourpassword");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);

using Office 365

MailMessage msg = new MailMessage();
...
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "yourpassword");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);

"It is never too late to be what you might have been"

Contact me!

Have questions? Feel free to use the form or send me an email with any questions.

I will be responsive to your requests and your questions.