In this tutorial, I will show a step-by-step procedure to employ Microsoft Graph API to authenticate and send mails from Microsoft Outlook (Enterprise Edition) with OAuth2 authentication mechanism. This article is organized as follows:
If you just want to run the demo program, please visit this GitHub repository to clone the sample program: https://github.com/duonghuuphuc/msgraph-sendmail-python.
To complete this tutorial, you will need to have the following things:
In this section, I will show you how to create an application on Microsoft Azure. First, you sign in to Microsoft Azure with either your personal account (Skype, Xbox, Live, and Hotmail) or work account (Microsoft 365 Business Plan). Then, you perform the following steps:
Credentials are used by confidential client applications that access a web API. In this example, we will add client secrets (a string) as credentials to our confidential client app registration.
In this section, we will add the corresponding API permissions to our application. By default, the User.Read.All
has been already added to the application. In this example, we will add more API permissions to allow our application to be able to send mails on the behalf of users.
The Microsoft identity platform supports two types of permissions, i.e., (a) delegated permissions and (b) application permissions.
In this example, we will add the following API permissions, the result is as shown in Fig. 6:
Mail.Send
(Delegated)Mail.Send
(Application) [Require an administrator to consent this permission]User.Read
(Delegated)User.Read.All
(Application) [Require an administrator to consent this permission]User.ReadBasic.All
(Delegated)In this section, I will present how to implement a Flask application (client program) that authenticates users and sends mails on the behalf of authenticated users. You need to perform the following step to run the demo program:
For ease of executing the demo program, I have prepared an env.sh file that contains a list of commands that run sequentially to start the demo Flask application. There are two parameters that you must configure before running the demo program, i.e., CLIENT_ID
and CLIENT_SECRET
. The CLIENT_ID
is a string that you did obtain as presented in Section 3. And the CLIENT_SECRET
is the recorded secret's value, as presented in Section 4.
After inserting the CLIENT_ID
and CLIENT_SECRET
into the env.sh file, you can now run the demo program by executing the following commands in Terminal window:
work <env_name>
pip install requirements.txt
source env.sh
You should notice that the second command is a one-time executed command. The first and third commands will be manually executed each time you run the demo program. Fig. 7 shows the result when visiting the http://localhost:5000 in a web browser.
The demo program allows you to sign in with both a personal account (Skype, Xbox, Live, and Hotmail) and a work account (Microsoft 365 Business plan). In Fig. 8, I demonstrate the demo program with a personal account.
After users click on the Yes button, they will be redirected to the demo application that will render a list of available functionalities, as shown in Fig. 9, since the users have been authenticated.
Fig. 10 shows the web page that demonstrates the send mail functionality which includes three textboxes, i.e., recipient email, subject and content. You can enter your email address to test this functionality.
In this article, I have presented the procedure to authorize a client program that sends emails on behalf of users that have either a personal Microsoft account or a business one. The procedure consists of four main steps, i.e., (1) create an application on Microsoft Azure, (2) issue credentials, (3) add API permissions to the application, and (4) run the demo program.