如何从EWS获取电子邮件时获取MimeContent?

时间:2016-05-31 06:38:47

标签: c# email exchangewebservices

我在我的应用程序中使用EWS服务。在这一点上,我想获取现有的电子邮件,然后将其转换为文件。我正在使用以下代码。

private ExchangeService _service = null;

internal EWSClient()
{
     _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
     _service.Credentials = new WebCredentials(HandlerSettings.MailReceiverLogin, HandlerSettings.MailReceiverPassword);
     _service.Url = new Uri(HandlerSettings.MailReceiverServer);
}

要获取电子邮件,我使用Bind方法。

var completeEmailMessage = EmailMessage.Bind(_service, emailId);
var mimeContent = completeEmailMessage.MimeContent;
string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".eml");
using (var fileStream = new FileStream(tempFile, FileMode.Create))
{
      fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
}

在这里,我得到例外,因为completeEmailMessage.MimeContent;是空的。

我应该使用哪种方法来获取包含MimeContent的完整电子邮件?

1 个答案:

答案 0 :(得分:0)

您应该创建一个PropertySet并将MIMEContent添加为要加载的属性,然后将其指定为Bind的重载属性,例如

        PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
        psPropSet.Add(ItemSchema.MimeContent);
        var completeEmailMessage = EmailMessage.Bind(_service, emailId, psPropSet);
        var mimeContent = completeEmailMessage.MimeContent;
        string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".eml");
        using (var fileStream = new FileStream(tempFile, FileMode.Create))
        {
            fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
        }