Click here to Skip to main content
15,868,158 members
Articles / Programming Languages / C#

C# SMTP Configuration for Outlook.Com SMTP Host

Rate me:
Please Sign up or sign in to vote.
4.94/5 (11 votes)
27 Dec 2013CPOL2 min read 130.2K   12   9
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: 
C#
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:

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

 to:

C#
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
C#
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:
C#
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

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer XIV Solutions
United States United States
My name is John Atten, and my username on many of my online accounts is xivSolutions. I am Fascinated by all things technology and software development. I work mostly with C#, Javascript/Node.js, Various flavors of databases, and anything else I find interesting. I am always looking for new information, and value your feedback (especially where I got something wrong!)

Comments and Discussions

 
QuestionThis uses basic auth, which is now deprecated. Pin
Member 1549784812-Jan-22 8:02
Member 1549784812-Jan-22 8:02 
QuestionHow do you apply this logic in your MVC web application Pin
gcogco106-Feb-20 2:24
gcogco106-Feb-20 2:24 
QuestionDoes this still work? Pin
Member 1224624826-Jul-19 5:47
Member 1224624826-Jul-19 5:47 
QuestionExcelent explanation! Pin
Douglas Acosta17-Aug-16 10:20
Douglas Acosta17-Aug-16 10:20 
QuestionThis program doesn't work now with outlook.com Pin
Stonica6-Oct-14 7:58
Stonica6-Oct-14 7:58 
AnswerRe: This program doesn't work now with outlook.com Pin
John Atten6-Oct-14 11:39
John Atten6-Oct-14 11:39 
GeneralMy vote of 5 Pin
Carsten V2.028-Dec-13 9:56
Carsten V2.028-Dec-13 9:56 
GeneralRe: My vote of 5 Pin
John Atten28-Dec-13 10:57
John Atten28-Dec-13 10:57 
QuestionVery good article! Pin
Volynsky Alex22-Dec-13 9:50
professionalVolynsky Alex22-Dec-13 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.