c# - 使用 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;
}
这段代码工作正常!!
有人可以分享我如何回复带有附件和邮件正文的邮件,就像我在发送邮件中所做的那样。
提前致谢。
解决方案
您必须创建回复,添加附件,然后发送消息。使用基本的基本 /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
希望能帮助到你。
推荐阅读
- lua - lua字符串连接得到一个奇怪的字符串
- java - 使用大集合或包含集合的等效地图是否很好?
- sql-server - 带有 SQL Server 后端和 pyodbc 的气流调度程序
- php - 如何在 Odoo Web 服务 API 上获取销售订单行描述?
- html - 如何让我的第三方脚本在我的网站上正常运行?
- unix - 有什么方法可以在 2 行中找到一个在另一个之下的相似词
- c++ - 如何正确接收多播UDP数据包?
- node.js - Mongoose:aggregate.lookup,部分结果未定义
- json - assemble.io 部分传递数据到嵌套的部分
- pentaho - 如何在 Pentaho 数据集成中动态更改“生成行步骤”的值字段,用于从 A REST API 获取数据