The Microsoft Graph .NET Mail Client Library targets .NET 6.0.
Integrate the Microsoft Graph API into your .NET project!
Search for MSGraph.Mail.Client
in the NuGet Library
OR
Type write below into the Package Manager Console.
Install-Package MSGraph.Mail.Client
-
Open a browser and navigate to the Microsoft Entra admin center and log in using a Work or School Account.
-
Register your application to use Microsoft Graph API using the Microsoft Application Registration Portal.
-
Select New registration. Enter a name for your application.
-
Set Supported account types as desired.
-
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.
-
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.
Note: If you want to use it for your personal email account like outlook.com or live.in, use tenantId
as consumer.
- Select Authentication under Manage. Locate the Advanced settings section and change the Allow public client flows toggle to Yes, then choose Save.
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);
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);
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);
The IEmailGraphService
interface provides three different APIs:
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.
Task<IList<EmailFileAttachment>> GetEmailAttachments(string? messageId);
- messageId: The email message ID received from the Graph API.
Task SendEmail(EmailMessage emailMessage);
- emailMessage: The email message object.
await emailGraphService.SendEmail(new EmailMessage
{
ToRecipients = new[] { "[email protected]" },
Subject = "Test Email",
BodyType = EmailBodyType.Html,
BodyContent = "<h1>Hello, World!</h1>"
});
Please check the EmailDemoApp
project for detailed examples.