首页 > 解决方案 > 使用 Microsoft 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;
}

这段代码工作正常!!

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

提前致谢。

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

解决方案


您必须创建回复添加附件,然后发送消息。使用基本的基本 /reply 端点,您无法做到这一点。

例如:

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

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

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

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

希望能帮助到你。


推荐阅读