Read the complete article on how to create and send messages using Microsoft Graph API in C#: https://blog.aspose.com/2022/04/18/manage-folders-using-ms-graph-api-in-csharp/
Last active
April 18, 2022 07:10
-
-
Save aspose-com-gists/1990e8661551f5ac642d4c08a35b7ceb to your computer and use it in GitHub Desktop.
Create and Send Messages using Microsoft Graph API in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate the access token | |
AccessTokenProvider tokenProvider = new AccessTokenProvider(); | |
// Create a Graph client | |
IGraphClient client = GraphClient.GetClient(tokenProvider, "tenant ID"); | |
// Create a new message | |
MapiMessage mm = new MapiMessage(); | |
mm.Subject = "EMAILNET-39318 " + Guid.NewGuid().ToString(); | |
mm.Body = "EMAILNET-39318 REST API v1.0 - Create Message"; | |
mm.SetProperty(KnownPropertyList.DisplayTo, "to@host.com"); | |
mm.SetProperty(KnownPropertyList.SenderName, "from"); | |
mm.SetProperty(KnownPropertyList.SentRepresentingEmailAddress, "from@host.com"); | |
// Create message in inbox folder | |
MapiMessage createdMessage = client.CreateMessage(Clients.Graph.KnownFolders.Inbox, mm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate the access token | |
AccessTokenProvider tokenProvider = new AccessTokenProvider(); | |
// Create a Graph client | |
IGraphClient client = GraphClient.GetClient(tokenProvider, "tenant ID"); | |
// Create a new message | |
MapiMessage mm = new MapiMessage(); | |
mm.Subject = "EMAILNET-39318 " + Guid.NewGuid().ToString(); | |
mm.Body = "EMAILNET-39318 REST API v1.0 - Create Message"; | |
mm.SetProperty(KnownPropertyList.DisplayTo, "to@host.com"); | |
mm.SetProperty(KnownPropertyList.SenderName, "from"); | |
mm.SetProperty(KnownPropertyList.SentRepresentingEmailAddress, "from@host.com"); | |
// Send message | |
client.Send(mm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccessTokenProvider : ITokenProvider | |
{ | |
DateTime expirationDate = DateTime.Now; | |
public void Dispose() | |
{ | |
} | |
public OAuthToken GetAccessToken(bool ignoreExistingToken) | |
{ | |
// Gets oAuth access token. | |
// If ignoreExistingToken is true, requests new token from a server. Otherwise behavior is depended on whether token exists or not. | |
// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server. | |
return null; | |
} | |
public OAuthToken GetAccessToken() | |
{ | |
// Gets oAuth access token. | |
// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server. | |
return new OAuthToken("token", expirationDate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment