Create draft message - Access is denied

Horacio Crespo 20 Reputation points
2023-01-12T23:55:16.3033333+00:00

Hello everyone,

I have a console application in C# for to read and send e-mails and it works fine. Now I'm implementing big attachments in the messages, so, I need to create a draft message for to add the files.

The sender is the principal queue where I delegated the other queues.

This is the code for to send the messages, and it works fine.

var result = await graphClient
	   .Users[sender]
	   .SendMail(message, saveToSentItems)
	   .Request()
	   .PostResponseAsync();

This is the code for to read the exchange e-mails with attachments, and it works fine.

private IMailFolderMessagesCollectionPage RetrieveMessages(string email, string folder, string filterByData, GraphServiceClient graphClient)
{
	var messages = graphClient
			 .Users[$"{email}"]
			 .MailFolders[$"{folder}"]
			 .Messages
			 .Request()
			 .Expand("attachments")
			 .Filter(filterByData)
			 .Top(1000)
			 .OrderBy("receivedDateTime asc")
			 .GetAsync()
			 .GetAwaiter()
			 .GetResult();
	return messages;
}

But if I add this part of code for to create a draft message

var draft = await graphClient.Users[sender].Messages
		.Request()
		.AddAsync(message);

It returns this error:

Code: ErrorAccessDenied

Message: Access is denied. Check credentials and try again.

User's image

This is my authentication method:

public static GraphServiceClient GetGraphClient()
{
	string clientId = ConfigurationManager.AppSettings["clientId"];
	string tenantId = ConfigurationManager.AppSettings["tenantId"];
	var scopes = new string[] { "https://graph.microsoft.com/.default" };

	var principalEmail = ConfigurationManager.AppSettings["PrincipalEmail"];
	var principalPassword = ConfigurationManager.AppSettings["PrincipalPassword"];           

	var options = new TokenCredentialOptions
	{
		AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
	};

	var userName = principalEmail;
	var password = principalPassword;
	var userNamePasswordCredential = new UsernamePasswordCredential(userName, password, tenantId, clientId, options);
	
	return new GraphServiceClient(userNamePasswordCredential, scopes);
}

I don't understand where the problem is, because I can send and read all e-mails, also I read e-mails in subfolders, so, I don't know where the problem is creating the draft message.

Does anyone have any idea what the problem could be?

Thanks in advance!

Horacio

Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. Gopinath Chennamadhavuni 2,446 Reputation points
    2023-01-16T09:44:18.8533333+00:00

    Hi @Horacio Crespo , glad to know that my suggestions have been fixed your issue.

    Rewriting my comments as an answer to this QnA post:

    After granting Mail.readwrite/Mail.readwrite.Shared permission to your application, you are able to create a draft message using MS Graph API.

    Please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.