microsoftgraphapimail
v1.0.1
Published
.net core sample console project to read mails from O365 account.
Downloads
30
Maintainers
Readme
MicrosoftGraphApiMail
.net core sample console project to read mails from O365 account.
About The Project
This is a sample .net core 3.1 console project to read mail in inbox folder from Office 365 account.
Prerequisites
- Register an app in Azure in Azure Active Directory
- Copy "Application (client) ID" and "Directory (tenant) ID"
- Assign "Mail.Read" and "User.Read.All" permission
- Remove "User.Read" it is not necessary
- An azure admin should grant these permissions for the app.
- Create a client secret.
- Copy client secret.
Installation
* npm
npm i microsoftgraphapimail
* init user-secrets
execute following commands
dotnet user-secrets init
dotnet user-secrets set appId "paste Application (client) ID from step 2"
dotnet user-secrets set tenantId "paste Directory (tenant) ID from step 2"
dotnet user-secrets set clientSecret "paste Client secret from step 7"
dotnet user-secrets set scopes "https://graph.microsoft.com/.default"
* read mails
// Initialize the auth provider with values from appsettings.json
var authProvider = new ClientSecretAuthProvider(appId, new[] { scopes }, tenantId, clientSecret);
// Request a token to sign in the user
var accessToken = authProvider.GetAccessToken().Result;
GraphHelper.Initialize(authProvider);
//type mail address which you want to read mails example: "[email protected]"
string mailAddress = "";
var messages = GraphHelper.GetInboxMessagesAsync(mailAddress).Result;
foreach (var message in messages)
{
System.Console.WriteLine(message.Sender.EmailAddress.Address);
System.Console.WriteLine(message.BodyPreview);
}