Send Emails using Microsoft Graph API and a desktop client
2021-12-14 Added more graph clients
2021-12-13 Updated to .NET 6, added calendar events, UserMailbox settings client
To send emails using Microsoft Graph API, you need to have an office license for the Azure Active Directory user which sends the email.
You can sign-in here to check this:
The API permissions require the Graph delegated permissions.
The Azure App registration requires the Graph API delegated Mail.Send and the Mail.ReadWrite scopes.
<appSettings>
<add key="AADInstance" value="https://login.microsoftonline.com/{0}/v2.0"/>
<add key="Tenant" value="--your-tenant--"/>
<add key="ClientId" value="--your-client-id--"/>
<add key="Scope" value="User.read Mail.Send Mail.ReadWrite"/>
</appSettings>
The Azure App registration requires the Graph API delegated User.Read.All and Presence.Read.All scope.
<appSettings>
<add key="AADInstance" value="https://login.microsoftonline.com/{0}/v2.0"/>
<add key="Tenant" value="--your-tenant--"/>
<add key="ClientId" value="--your-client-id--"/>
<add key="Scope" value="User.read User.Read.All Presence.Read.All"/>
</appSettings>
The Azure App registration requires the Graph API delegated User.Read.All MailboxSettings.Read scopes.
<appSettings>
<add key="AADInstance" value="https://login.microsoftonline.com/{0}/v2.0"/>
<add key="Tenant" value="--your-tenant--"/>
<add key="ClientId" value="--your-client-id--"/>
<add key="Scope" value="User.read User.Read.All MailboxSettings.Read"/>
</appSettings>
The Azure App registration requires the Graph API delegated User.Read.All **Calendars.Read Calendars.Read.Shared scopes.
<appSettings>
<add key="AADInstance" value="https://login.microsoftonline.com/{0}/v2.0"/>
<add key="Tenant" value="--your-tenant--"/>
<add key="ClientId" value="--your-client-id--"/>
<add key="Scope" value="User.read User.Read.All Calendars.Read Calendars.Read.Shared"/>
</appSettings>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.39.0" />
<PackageReference Include="Microsoft.Graph" Version="4.11.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
I converted the project from C# to Visual Basic using the telerik tool:
https://converter.telerik.com/
https://docs.microsoft.com/en-us/graph/outlook-send-mail-from-other-user
https://stackoverflow.com/questions/43795846/graph-api-daemon-app-with-user-consent
https://winsmarts.com/managed-identity-as-a-daemon-accessing-microsoft-graph-8d1bf87582b1
https://cmatskas.com/create-a-net-core-deamon-app-that-calls-msgraph-with-a-certificate/
https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http
https://stackoverflow.com/questions/56110910/sending-email-with-microsoft-graph-api-work-account
https://docs.microsoft.com/en-us/graph/api/user-get-mailboxsettings?view=graph-rest-1.0&tabs=csharp
https://docs.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=csharp
For more information see MSAL.NET's conceptual documentation:
- Quickstart: Register an application with the Microsoft identity platform
- Quickstart: Configure a client application to access web APIs
- Recommended pattern to acquire a token in public client applications
- Acquiring tokens interactively in public client applications
- Customizing Token cache serialization