microsoftgraphapimail

1.0.1 • Public • Published

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

  1. Register an app in Azure in Azure Active Directory

1

2

  1. Copy "Application (client) ID" and "Directory (tenant) ID"

3

  1. Assign "Mail.Read" and "User.Read.All" permission

4

  1. Remove "User.Read" it is not necessary

5

  1. An azure admin should grant these permissions for the app.

6

  1. Create a client secret.

7

8

  1. Copy client secret.

9

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: "ertugrul.balveren@balsoft.de"
  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);
  }  
  

Package Sidebar

Install

npm i microsoftgraphapimail

Weekly Downloads

15

Version

1.0.1

License

MIT

Unpacked Size

10.5 MB

Total Files

51

Last publish

Collaborators

  • ertgrlbalveren