Using Microsoft Hotmail/LIVE OAUTH + SMTP


Microsoft Live SMTP servers (Hotmail, Oultook personal account) have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Microsoft Web OAuth instead of inputting user and password directly in application. This way is more secure, but a little bit complex.


Create your application in Azure Portal

To use Microsoft/Office365 OAUTH in your application, you must create a application in https://portal.azure.com.


Single tenant and multitenant in account type

When the register an application page appears, enter a meaningful application name and select the account type.

Select which accounts you would like your application to support.

Because we want to support all Office 365 and LIVE SDK (hotmail, outlook personal account), so select Accounts in any organizational directory and personal Microsoft accounts.


API Permission

Now we need to add permission to the application:

Click API Permission -> Add a permission -> Microsoft Graph -> Delegated Permission -> User.Read, email, offline_access, openid, profile, SMTP.Send, IMAP.AccessAsUser.All, POP.AccessAsUser.All.

EWS API permission

With the above permissions, your application can support SMTP, POP and IMAP service. If your application needs to support EWS protocol either, add EWS permission like this:

Click API Permission -> Add a permission -> APIs in my organization uses -> Office 365 Exchange Online -> Delegated Permission -> Check EWS.AccessAsUser.All

Here is permissions list:


Authentication and redirect uri


Client Id and client secrets

Now we need to create a client secret for the application, click Certificates and secrets -> client secrets and add a new client secret.

After client secret is created, store the client secret value to somewhere, Please store client secret value by yourself, because it is hidden when you view it at next time.


Branding and verify publisher

Now we click Branding, you can edit your company logo, URL and application name. If your application supports multitenant (access user in all Office 365 and Microsoft personal account), you must complete the publisher verification.

It is not difficult, you can have a look at publisher verification. After publisher verification is completed, your branding is like this:

You must complete the publisher verification for multitenant application, otherwise, your application will not request access token correctly.


Client id and tenant

Now you can click Overview to find your client id and tenant.

If your application is single tenant, use the tenant value in tokenUri and authUri instead of "common". If your application is multitenant, use "common" as tenant.

Above client_id and secret support both "Office365 + SMTP/POP/IMAP/EWS" and "Live (hotmail, outlook personal account) + SMTP/POP/IMAP".


Use client id and client secret to request access token

You can use client id and client secret to get the user email address and access token like this:


Access token expiration and refresh token

You don’t have to open browser to request access token every time. By default, access token expiration time is 3600 seconds, you can use the access token repeatedly before it is expired. After it is expired, you can use refresh token to refresh access token directly without opening browser. You can find full sample project in EASendMail installation path to learn how to refresh token.

You should create your client id and client secret, don't use the client id in the sample project, it is only for test purpose. If you got "This app isn't verified" information, please click "advanced" -> Go to ... for test.


Use access token to send email with Live/Hotmail SMTP server

After you get user email address and access token, you can use the following codes to send email using Hotmail/Live OAUTH + SMTP protocol.

Example

[Visual Basic, C#, C++] To get the full samples of EASendMail, please refer to Samples section.

[VB6 - Send Email using Microsoft Hotmail OAUTH Authentication]

Const ConnectNormal = 0
Const ConnectSSLAuto = 1
Const ConnectSTARTTLS = 2
Const ConnectDirectSSL = 3
Const ConnectTryTLS = 4

Const AuthAuto = -1
Const AuthLogin = 0
Const AuthNtlm = 1
Const AuthCramMd5 = 2
Const AuthPlain = 3
Const AuthMsn = 4
Const AuthXoauth2 = 5

Private Sub SendEmail(email As String, access_token As String)
    Dim oSmtp As EASendMailObjLib.Mail
    Set oSmtp = New EASendMailObjLib.Mail
    oSmtp.LicenseCode = "TryIt"

    ' Hotmail SMTP server
    oSmtp.ServerAddr = "smtp.live.com"
    oSmtp.ServerPort = 587

    ' Enable SSL/TLS connection
    oSmtp.ConnectType = ConnectSSLAuto

    ' OAUTH/XOAUTH2 type
    oSmtp.AuthType = AuthXoauth2 
    oSmtp.UserName = email
    oSmtp.Password = access_token
    
    oSmtp.FromAddr = email
    oSmtp.AddRecipient "Support Team", "support@emailarchitect.net", 0
    
    oSmtp.BodyText = "Hello, this is a test...."
    If oSmtp.SendMail() = 0 Then
    MsgBox "Message delivered!"
    Else
    MsgBox oSmtp.GetLastErrDescription()
    End If

End Sub
        

[Visual C++ - SMTP Microsoft Hotmail OAUTH User Authentication] #include "stdafx.h" #include <tchar.h> #include <Windows.h> #include "EASendMailObj.tlh" using namespace EASendMailObjLib; const int ConnectNormal = 0; const int ConnectSSLAuto = 1; const int ConnectSTARTTLS = 2; const int ConnectDirectSSL = 3; const int ConnectTryTLS = 4; const int AuthAuto = -1; const int AuthLogin = 0; const int AuthNtlm = 1; const int AuthCramMd5 = 2; const int AuthPlain = 3; const int AuthMsn = 4; const int AuthXoauth2 = 5; void SendEmail(const TCHAR* lpszEmail, const TCHAR* lpszAccessToken) { ::CoInitialize(NULL); IMailPtr oSmtp = NULL; oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail)); oSmtp->LicenseCode = _T("TryIt"); // Hotmail SMTP server oSmtp->ServerAddr = _T("smtp.live.com"); oSmtp->ServerPort = 587; // Enable SSL/TLS connection oSmtp->ConnectType = ConnectSSLAuto; // OAUTH/XOAUTH2 type oSmtp->AuthType = AuthXoauth2; oSmtp->UserName = lpszEmail; oSmtp->Password = lpszAccessToken; oSmtp->FromAddr = lpszEmail; oSmtp->AddRecipient(_T("Support Team"), _T("support@emailarchitect.net"), 0); oSmtp->BodyText = _T("Hello, this is a test...."); if (oSmtp->SendMail() == 0) _tprintf(_T("Message delivered!")); else _tprintf((const TCHAR*)oSmtp->GetLastErrDescription()); }
[Delphi - SMTP Microsoft Hotmail OAUTH User Authentication] const ConnectNormal = 0; ConnectSSLAuto = 1; ConnectSTARTTLS = 2; ConnectDirectSSL = 3; ConnectTryTLS = 4; AuthAuto = -1; AuthLogin = 0; AuthNtlm = 1; AuthCramMd5 = 2; AuthPlain = 3; AuthMsn = 4; AuthXoauth2 = 5; procedure TForm1.SendEmail(email: string, accesstoken: string); var oSmtp : TMail; begin oSmtp := TMail.Create(Application); oSmtp.LicenseCode := 'TryIt'; // Hotmail SMTP server address oSmtp.ServerAddr := 'smtp.live.com'; oSmtp.ServerPort := 587; // Enable SSL/TLS connection oSmtp.ConnectType := ConnectSSLAuto; // OAUTH/XOAUTH2 type oSmtp.AuthType := AuthXoauth2; oSmtp.UserName := email; oSmtp.Password := accesstoken; // Set sender email address oSmtp.FromAddr := email; // Add recipient email address oSmtp.AddRecipientEx('support@emailarchitect.net', 0); // Set email subject oSmtp.Subject := 'simple email from Delphi project'; // Set email body oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply'; ShowMessage('start to send email ...'); if oSmtp.SendMail() = 0 then ShowMessage('email was sent successfully!') else ShowMessage('failed to send email with the following error: ' + oSmtp.GetLastErrDescription()); end; end.

Remarks

If you don't want to use OAUTH 2.0, Hotmail/Live also supports traditional user authentication.

Online Example

VB6 - Send Email using Google/Gmail OAuth 2.0 Authentication
VB6 - Send Email using Gmail/G Suite OAuth 2.0 in Background Service (Service Account)
VB6 - Send Email using Microsoft OAuth 2.0 (Modern Authentication) from Hotmail/Outlook Account
VB6 - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 Account
VB6 - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 in Background Service

Delphi - Send Email using Google/Gmail OAuth 2.0 Authentication
Delphi - Send Email using Gmail/G Suite OAuth 2.0 in Background Service (Service Account)
Delphi - Send Email using Microsoft OAuth 2.0 (Modern Authentication) from Hotmail/Outlook Account
Delphi - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 Account
Delphi - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 in Background Service

Visual C++ - Send Email using Google/Gmail OAuth 2.0 Authentication
Visual C++ - Send Email using Gmail/G Suite OAuth 2.0 in Background Service (Service Account)
Visual C++ - Send Email using Microsoft OAuth 2.0 (Modern Authentication) from Hotmail/Outlook Account
Visual C++ - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 Account
Visual C++ - Send Email using Microsoft OAuth 2.0 (Modern Authentication) + EWS Protocol from Office 365 in Background Service

See Also

Using EASendMail ActiveX Object
Registration-free COM with Manifest File
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail SMTP OAUTH
Using Gmail/GSuite Service Account + SMTP OAUTH Authentication
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail SMTP OAUTH
From, ReplyTo, Sender and Return-Path
Digital Signature and Email Encryption - S/MIME
DomainKeys Signature and DKIM Signature
Send Email without SMTP server(DNS lookup)
Work with EASendMail Service(Mail Queuing)
Programming with Asynchronous Mode
Programming with FastSender
Mail vs. FastSender
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking
Work with RTF and Word
EASendMail ActiveX Object References
EASendMail SMTP Component Samples