MSGraph.Mail.Client 1.1.0

dotnet add package MSGraph.Mail.Client --version 1.1.0
                    
NuGet\Install-Package MSGraph.Mail.Client -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MSGraph.Mail.Client" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MSGraph.Mail.Client" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MSGraph.Mail.Client" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MSGraph.Mail.Client --version 1.1.0
                    
#r "nuget: MSGraph.Mail.Client, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=MSGraph.Mail.Client&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MSGraph.Mail.Client&version=1.1.0
                    
Install as a Cake Tool

Microsoft Graph .NET Client Library For Mail

NuGet Version NuGet Downloads License

The Microsoft Graph .NET Mail Client Library targets .NET 6.0.

Integrate the Microsoft Graph API into your .NET project!

Installation via NuGet

Search for MSGraph.Mail.Client in the NuGet Library

OR

Type write below into the Package Manager Console.

Install-Package MSGraph.Mail.Client

Getting started

1. Register your application

  1. Open a browser and navigate to the Microsoft Entra admin center and log in using a Work or School Account.

  2. Register your application to use Microsoft Graph API using the Microsoft Application Registration Portal.

  3. Select New registration. Enter a name for your application.

  4. Set Supported account types as desired.

  5. If you want to set up your application as a background service, leave the Redirect URI empty. Otherwise, if you want to set it up as a delegate (sign-in) user, add http://localhost.

  6. Select Register. On the application's Overview page, copy the value of the Application (client) ID and save it. You will need it in the next step. If you chose Accounts in this organizational directory only for Supported account types, also copy the Directory (tenant) ID and save it.

Application Registration

Note: If you want to use it for your personal email account like outlook.com or live.in, use tenantId as consumer.

  1. Select Authentication under Manage. Locate the Advanced settings section and change the Allow public client flows toggle to Yes, then choose Save.

Allow Public Client Flows

2. Create a Microsoft Graph mail client object with an authentication provider

Interactive Flow
Settings settings = new();
settings.ClientId = "";
settings.TenantId = "";
// We need to pass the scopes which we require and the same has been set at the API Permission in Azure
settings.GraphUserScopes = new string[] {
  "openid",
  "profile",
  "offline_access",
  "user.read",
  "mail.readbasic",
  "mail.read",
  "mail.send"
};

IAuthenticationProvider authenticationProvider = new AuthenticationInteractiveProvider(settings);
Daemon or Client Secret Flow
Settings settings = new();
settings.ClientId = "";
settings.TenantId = "";
settings.SecretId = "";

// We set the scope only in the API permission level in Azure.
IAuthenticationProvider authenticationProvider = new AuthenticationClientSecretProvider(settings);

3. Make requests to the graph mail

Once you have completed authentication, you can begin to make calls to the Email Graph service. The requests in the SDK follow the format of the Microsoft Graph Mail API's RESTful syntax.

IEmailGraphService emailGraphService = new EmailGraphService(authenticationProvider);

4. Read and Send Emails

The IEmailGraphService interface provides three different APIs:

Get Emails
Task<IList<EmailMessage>> GetEmailsAsync(
    int top = 10, 
    int limit = 10, 
    EmailRequestParameterInformation? requestInformation = null, 
    bool markRead = false
);
  • top: Set the top parameter to fetch only that many emails. By default, it fetches the top 10 emails in descending order by receivedDateTime.
  • limit: Limit the number of emails fetched. Set limit to -1 to fetch all emails.
  • requestInformation: Helps set $filter, $search, order by given fields, and includes attachments with emails.
Get Email Attachments
Task<IList<EmailFileAttachment>> GetEmailAttachments(string? messageId);
  • messageId: The email message ID received from the Graph API.
Send Email
Task SendEmail(EmailMessage emailMessage);
  • emailMessage: The email message object.

Example

await emailGraphService.SendEmail(new EmailMessage
{
    ToRecipients = new[] { "example@example.com" },
    Subject = "Test Email",
    BodyType = EmailBodyType.Html,
    BodyContent = "<h1>Hello, World!</h1>"
});

Note

Please check the EmailDemoApp project for detailed examples.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 52,642 5/24/2024
1.0.0 135 4/28/2024