1

I've been working with Microsoft graph API to receive and reply to the mail. I've successfully received and send mails, but as per Graph API docs in reply only a comment can be passed. https://learn.microsoft.com/en-us/graph/api/message-createreply?view=graph-rest-1.0&tabs=cs

I've developed the send mail code as shown below:-

IList<Recipient> messageToList = new List<Recipient>();
User currentUser = client.Me.Request().GetAsync().Result;

Recipient currentUserRecipient = new Recipient();

EmailAddress currentUserEmailAdress = new EmailAddress();

EmailAddress recepientUserEmailAdress = new EmailAddress();
currentUserEmailAdress.Address = currentUser.UserPrincipalName;

currentUserEmailAdress.Name = currentUser.DisplayName;
messageToList.Add(currentUserRecipient);
try

{

                ItemBody messageBody = new ItemBody();

                messageBody.Content = "A sample message from Ashish";

                messageBody.ContentType = BodyType.Text;


                Message newMessage = new Message();

                newMessage.Subject = "\nSample Mail From Ashish.";
                newMessage.ToRecipients = messageToList;
                newMessage.CcRecipients = new List<Recipient>()
                    {
                        new Recipient
                        {
                            EmailAddress = new EmailAddress
                            {
                                Address = "[email protected]"
                            }
                        }
                    };
                newMessage.Body = messageBody;




                client.Me.SendMail(newMessage, true).Request().PostAsync();
                Console.WriteLine("\nMail sent to {0}", currentUser.DisplayName);

}
catch (Exception)
{
    Console.WriteLine("\nUnexpected Error attempting to send an email");
    throw;
}

This code is working fine!!

Can someone please share how I can Reply to a mail with attachment and mailbody like I'm able to do in Send mail.

Thanks in advance.

1 Answer 1

1

You have to create a reply, add the attachment, and then send the message. With the basic basic /reply endpoint you cant do it.

E.g.:

  • Create the message draft using POST request

As a response you will get the whole message structure with id set to something like AQMkADAwATMwMAItMTJkYi03YjFjLTAwAi0wMAoARgAAA_hRKmxc6QpJks9QJkO5R50HAP6mz4np5UJHkvaxWZjGproAAAIBDwAAAP6mz4np5UJHkvaxWZjGproAAAAUZT2jAAAA. Lets refer to it as {messageID}.

  • After that you can create an attachment using POST request to https://graph.microsoft.com/beta/me/messages/{messageID}/attachments

-After step 2 you will see created message in your mailbox Drafts folder. To send it use https://graph.microsoft.com/beta/me/messages/{messageID}/send

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

can we get a working example. As i Have all permission and followed the above steps, still getting error as " ErrorSendAsDenied Message: The user account which was used to submit this request does not have the right to send mail on behalf of the specified sending account., Cannot submit message"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.