Mrudang Dalal
Mrudang Dalal

Reputation: 121

How to get MimeContent while fetching an email from EWS?

I am using EWS service in my Application. At point I want fetch an existing email and then convert it to a file. i am using following code for this.

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);
}

To Fetch email I use Bind method.

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);
}

Here, i get exception as completeEmailMessage.MimeContent; is null.

Which method shall i use to get complete email having MimeContent ?

Upvotes: 1

Views: 1639

Answers (1)

Glen Scales
Glen Scales

Reputation: 22032

You should create a PropertySet and add the MIMEContent as a property to load then specify that as an overload property for Bind eg

        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);
        }

Upvotes: 2

Related Questions