How to send emails using Microsoft Graph API in .NET 6 + 7 as a background service

Do you need to send emails using Microsoft Graph in .NET? Have you ever been in the need for sending emails from your .NET application? In the past, we used to rely on SMTP clients but SMTP with basic authentication is not that secure anymore 🔓.

As of October 2022, Microsoft has disabled basic authentication for security reasons and we as developers have to comply with that in order to make our applications send emails if we use Microsoft as our service provider for sending emails 📧.

"Back in the day" sending an email from .NET was very easy using the SMTP protocol (Simple Mail Transfer Protocol) using the SmtpClient class or by using MailKit in combination with SMTP.

How to Send Emails with ASP.NET Core using MailKit + SMTP
Learn how to send emails with ASP.NET Core using MailKit and an SMTP Server + how to send emails based on templates using Razor.

Microsoft has been telling developers for a few years that its SmtpClient is no longer recommended and Microsoft encourages developers to switch to a modern approach.

The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.

You can read the full remarks section below.

SmtpClient Class (System.Net.Mail)
Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.

Basic authentication has been disabled and we usually used that in combination with the SmtpClient. How do we move forward and make sure that our applications are able to send emails today, tomorrow, and next week from Office 365? 🤷‍♂️ Microsoft Graph is the answer to that 🙌.

Microsoft Graph

In short - Microsoft Graph is a set of restful HTTP APIs that allows us to interact with Microsoft 365. The API is capable of sending, reading, etc... emails on the Microsoft 365 platform (for your reference Office 365 is becoming Microsoft 365).

Microsoft Graph Dev Center | APIs and app development
Connect to Microsoft Graph data and other datasets to derive insights and analytics, extend Microsoft 365 experiences, and build unique, intelligent apps.
⚠️
Exchange Online limits sending to more than 10.000 emails per day. You can learn more about the limits here. If you need to send more than 10.000 emails per day, you can use the new Azure Communication Services.

In order for our application to send emails without the user interacting we will configure it to run as a daemon. For the program to interact with Microsoft Graph we need to authenticate with an OAuth Access Token that has permissions defined in Azure.

If we were to develop an application where the user grants access to their account it would be easier as the user can make the request for the permissions by themself and allow the program to send emails using delegated permissions. However, that is not the case for this tutorial. We wanna do this as a background service/ a daemon.

In case we need to run this as a background service we can use an App Registration in Azure AD that already has the permissions assigned. The App Registration will provide us with a client id and client secret (you can see them as your old basic credentials), these details can be used to obtain a token from Microsoft 365.

How does that work? It requires some setup in Azure and you might need an administrator to accomplish the task (if you are located in an organization where the IT department has full control of your Azure tenant.)

Register and Configure an application in Azure

To get started you have to sign in to Azure and navigate to Azure AD. Click on App Registration in the menu on your left and click the + New Registration button in the window.

register azure application, azure, send emails using microsoft graph
Register an application in Azure AD

You can enter a name describing your application at a high level. The rest of the settings can you leave as default - our application is not using them.

This post is for subscribers only

Sign up to read this post and all other free member only posts. Yes it's totally free and I promise no spam - you can unsubscribe or modify what kind of notifications you get from TWC at any time.
You've successfully subscribed to Tech with Christian
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.