Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# SMTP Configuration for Outlook.Com SMTP Host

0.00/5 (No votes)
27 Dec 2013 1  
C# SMTP configuration for Outlook.com SMTP host

3306827131_eff5401a63If you want to send Email programmatically using your Outlook.com or Gmail account as the SMTP host, there are a few things to pay attention to in order to get it all working.

Using the basic System.Net.Mail library, sending email is generally fairly straightforward. However, if you want to send using your Outlook.Com or Gmail account as the SMTP host, you will most likely need to take a few extra steps, if you have two-stage authorization enabled (and you DO have two-stage auth enabled, RIGHT??!!).  

Image by JASE Group LLC | Some Rights Reserved  

SMTP Configuration Example for Outlook.Com SMTP Host

Here is a basic class with SMPT configuration for sending mail using Outlook.Com SMTP:

Basic Mail Configuration Settings for Outlook.Com SMTP: 
using System;
  
// You will need to add a reference to this library:
using System.Net.Mail;
  
namespace SmtpMailConnections
{
    public class OutlookDotComMail
    {
        string _sender = "";
        string _password = "";
        public OutlookDotComMail(string sender, string password)
        {
            _sender = sender;
            _password = password;
        }  
  
        public void SendMail(string recipient, string subject, string message)
        {
            SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
  
            client.Port = 587;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential credentials = 
                new System.Net.NetworkCredential(_sender, _password);
            client.EnableSsl = true;
            client.Credentials = credentials;
  
            try
            {
                var mail = new MailMessage(_sender.Trim(), recipient.Trim());
                mail.Subject = subject;
                mail.Body = message;
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
    }
}

As you can see, we have kept this pretty minimal for the purpose of clarity. We initialize our simple class with a user name (in this case, our Outlook.Com email address) and a password.

Note that the above, and all of the following examples can be modified to work with Gmail by changing:

 SmtpClient client = new SmtpClient("smtp-mail.outlook.com"); 

 to:

SmtpClient client = new SmtpClient("smtp.gmail.com"); 

 Example Usage

We can call into this class to send mail like this (this example is a simple console application):

Sending Mail Using the Mail Sender Class
class Program
{
    static void Main(string[] args)
    {
        string mailUser = "YourAccount@outlook.com";
        string mailUserPwd = "YourPassword";
  
        var sender = new OutlookDotComMail(mailUser, mailUserPwd);
        sender.SendMail("recipient@example.com", "Test Mail", "Hello!");
    }
}

If you run the code above, using your own Outlook.Com Email address and password, all should work fine.

Unless you have enabled two-stage authorization on your Outlook.Com account. If you have, you will need to create an application specific password, or the code above will throw an exception when your credentials are refused by the Outlook.Com SMTP server.

Create an App-Specific Password if You Have 2-Stage Auth Enabled

To create an App-Specific Password, log in to your Outlook.com account, and go to Account Settings –> Security Info –> App Passwords:

outlook-com-app-passwords

Click on the Create a new app password link, and voila – you now have a new password for use within your application:

create-new-app-password

Use this as the password in your code, and all should be well:

Use the App Password Instead of Your Outlook.Com Account Password:
class Program
{
    static void Main(string[] args)
    {
        string mailUser = "YourAccount@outlook.com";
        string mailUserPwd = "bnppnnenfmpiixty";
  
        var sender = new OutlookDotComMail(mailUser, mailUserPwd);
        sender.SendMail("recipient@example.com", "Test Mail", "Hello!");
    }
}

 

 The above applies to Gmail two-step auth as well. If you have two-step auth enabled, you will need to create an app-specific password.  

You can find the code samples above at my Github repo.

Additional Resources and Items of Interest

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here