C#

C# SMTP Configuration for Outlook.Com SMTP Host


Image by JASE Group LLC | Some Rights Reserved

If 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??!!).

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, out Outlook.Com email address) and a password.

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!");
    }
}

You can find the Code Samples above at my Github repo.

Additional Resources and Items of Interest

ASP.Net
ASP.NET Identity 2.0: Setting Up Account Validation and Two-Factor Authorization
ASP.Net
ASP.NET MVC 5 Identity: Implementing Group-Based Permissions Management Part II
ASP.Net
ASP.NET MVC and Identity 2.0: Understanding the Basics
There are currently no comments.