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

标签 c# web-services email class

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

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/

相关文章:

HTML 电子邮件 - 为什么背景图像未加载?

c# - .NET 字符串性能问题

ios - 从 IMAP 服务器检索邮件?

html - Evolution 电子邮件客户端中的 CSS 支持

javascript - 如何在 jQuery.post 中使用动态数据名称?

c# - 从 WSDL C# 生成 Web 服务代理

php - MySQL 查询代码对 Web 服务的两个不同字段使用 OR

c# - Parallel.ForEach 停止为最后几个项目并行

C#/WPF 使用异步和等待,但 UI 线程仍然被阻塞

c# - 在 ASP .Net 3.5 中使用 Ninject 2.0