gpt4 book ai didi

c# - 使用 Emailmessage 类检索邮件正文

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:56 24 4
gpt4 key购买 nike

我必须检索已发送电子邮件的正文并将其存储在共享文件夹中。我有以下代码示例来从电子邮件中检索附件并存储它。

EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Id.ToString()),
new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

foreach (Attachment attachment in message.Attachments)
{

if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;

// Load attachment contents into a file.
fileAttachment.Load("C:\\CodeCopy\\Email\\temp\\" + fileAttachment.Name);

同样,如果我想使用 EmailMessage.body 属性,我该如何使用它。我是初学者,所以请给出详细的答案。

最佳答案

您可以使用 EmailMessage.Body属性获取表示消息正文的对象,然后调用其 ToString()方法将正文内容作为字符串获取,并将该字符串写入文件:

using System.IO;
using System.Text;
using Microsoft.Exchange.WebServices.Data;

EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Id.ToString()),
new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
using (StreamWriter writer = new StreamWriter(String.Format(
CultureInfo.InvariantCulture, @"C:\CodeCopy\Email\temp\{0}.body.txt",
item.Id.ToString().Replace('\\', '_')))) {
writer.Write(message.Body.ToString());
}

关于c# - 使用 Emailmessage 类检索邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com