Email Sender is a convenient way to test email connectivity. Lately, SMTP Authentication using legacy mechanisms has been discouraged in favor of OAuth2. This page discusses steps for creating an OAuth-compatible Access Token and then using that token to send an email. There are no prerequisites for this article. It assumes you have not used OAuth in the past. Most folks are familiar with a user ID and password. This article explains how you can transition from that concept to OAuth2.
Let's discuss some key concepts in layman's terms.
The most challenging part of using OAuth is obtaining and renewing Access Tokens. This is usually done using APIs that adhere to RFC 6749. However, we will use Postman, a freely available software, to simplify matters. Moreover, we will use a free account from Microsoft Azure to create this Access Token. Here are step-by-step instructions.
You will need Postman to send requests to Microsoft. You don't have to create an account in Postman if you don't want to save your session, which is fine for the purpose of this article.
Our goal is to use Microsoft's SMTP server to route emails to recipients online, which requires creating an account. Visit Azure's home page to do that.
Many services offered in Azure are free. Although, Microsoft will ask you to provide your credit card number when creating this account, your card will not be charged.
Let's say user creating this account is identified by jack@hotmail.com
.
This step follows and summarizes instructions on this page published on Microsoft's website.
You must use the third option if you want the sender of this email to be any Hotmail/Outlook.com user. Consider the following scenario that will require you to choose this option.
Imagine you're working on an application that relies on John, who has a free Hotmail account, to sign in using his credentials. Once John signs in, you want to invite his friend Jane via email. You aim to send this invitation on John's behalf using his email as the sender.
To recap, you're registering a third-party application asking an unknown person (John) to use his credentials before emailing Jane.
https://www.getpostman.com/oauth2/callback
for the URL as shown below.This callback URL will refer to your application after you replace Postman.
Open Postman and click the New on the left. Then, select HTTP for the protocol.
Next, click Authorization, and select OAuth 2.0 as the Type, as shown below.
The following table describes values that go in different fields in Postman:
Field Name | Description |
---|---|
Grant Type: | Select Authorization Code from the list. |
Callback URL: | This must be same as you put during the App registration, which is https://www.getpostman.com/oauth2/callback |
Auth URL: | Switch to the screen displaying Azure configuration, click Endpoints, and copy/paste the
value from OAuth 2.0 authorization endpoint (v2) as shown below.
|
Access Token URL | Copy/paste the values from the OAuth 2.0 token endpoint (v2) displayed in Azure configuration. |
Click ID | This is the Application (client) ID field in Azure. |
Client Secret | This value was created in step 3 when you create a secret. |
Scope | Enter https://outlook.office.com/SMTP.Send for this field. This value was
copied from this page. |
Client Authentication | Select Send as Basic Auth header |
Click the Get New Access Token button when ready. This will open a browser-like window prompting you to sign-in to a Microsoft account.
jack@hotmail.com
. Instead, you can sign in using any account hosted on Microsoft, free or paid.
Once done, you will be displayed your new Access Token, as displayed in the image below.
Copy the characters you see on this screen.
Now you're ready to send an email. Download Email Sender and run it on your machine.
You will have to copy/paste the newly created Access Token in the password field and check the box for OAuth,
as shown below.
The following steps occur in the background after you click the button to fetch a new Access Token in Postman.
As mentioned earlier, Access Tokens are short-lived and must be refreshed after some time. As a result, when Microsoft sends the Access Token for the first time, it also includes a Refresh Token. The client application is expected to store this refresh token in a safe place. Refresh tokens only expire if there is no activity for a long time (~ six months). Therefore, applications can use them to pull new Access Tokens.