使用Emailmessage类检索邮件正文

时间:2011-01-31 10:10:45

标签: 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属性,我该如何使用它。 我是初学者,所以请详细解答。

1 个答案:

答案 0 :(得分:1)

您可以使用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());
}