使用Microsoft Graph API发送带有附件的回复

时间:2019-06-20 06:36:45

标签: c# asp.net-web-api azure-ad-graph-api

我一直在使用Microsoft graph API来接收和回复邮件。 我已经成功接收和发送了邮件,但是根据Graph API文档的回复,只能传递评论。 https://docs.microsoft.com/en-us/graph/api/message-createreply?view=graph-rest-1.0&tabs=cs

我开发了如下所示的发送邮件代码:-

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 = "abc.xyz@xxxx.com"
                            }
                        }
                    };
                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;
}

此代码运行正常!

有人可以像我在发送邮件中一样分享我如何回复带有附件和邮件正文的邮件。

谢谢。

1 个答案:

答案 0 :(得分:0)

您必须先create a replyadd the attachment,然后再send the message。使用基本的基本/ reply端点,您将无法做到。

例如:

  • 使用POST请求创建消息草稿

作为响应,您将获得ID设置为AQMkADAwATMwMAItMTJkYi03YjFjLTAwAi0wMAoARgAAA_hRKmxc6QpJks9QJkO5R50HAP6mz4np5UJHkvaxWZjGproAAAIBDwAAAP6mz4np5UJHkvaxWZjGproAAAAUZT2jAAAA之类的整个消息结构。让我们将其称为{messageID}。

  • 之后,您可以使用对https://graph.microsoft.com/beta/me/messages/{messageID}/attachments的POST请求创建附件。

-步骤2之后,您将在邮箱“草稿”文件夹中看到已创建的消息。要发送它,请使用https://graph.microsoft.com/beta/me/messages/{messageID}/send

希望有帮助。