EWS Managed WEB API Retrieve HTML and Text Email in a single call

1.2k views Asked by At

I am retrieving emails using EWS web API and below is my code

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

    service.Credentials = new WebCredentials(EWSUserName, EWSPassword);

    service.TraceEnabled = true;
    service.TraceFlags = TraceFlags.All;

    service.AutodiscoverUrl(EWSUserName, RedirectionUrlValidationCallback);


    PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients, EmailMessageSchema.Sender,
                    EmailMessageSchema.ConversationId, EmailMessageSchema.Id);
    itempropertyset.RequestedBodyType = BodyType.HTML;
  FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100),);
    ServiceResponseCollection<GetItemResponse> items =
                service.BindToItems(findResults.Select(item => item.Id), itempropertyset);


    MailItem[] mailItemsDetail = items.Select(item =>
    {
        return new MailItem()
        {
            From = ((Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[EmailMessageSchema.From]).Address,
            Recipients = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(),
            Subject = item.Item.Subject,
            Body = item.Item.Body.ToString(),
            DateTimeSent = item.Item.DateTimeSent,
            EmailMessageID = item.Item.Id.UniqueId,
        };

    }).ToArray();

I have to use BodyType.HTML or BodyType.Text to retrieve the email content , but I need a single call to retrieve both type (Text and HTML ).

How can I retrieve HTML as well as Text body in a single call?

1

There are 1 answers

0
Michael Mainer On

Assuming that you are targeting Exchange 2013+, I think you can get the text body if you add ItemSchema.TextBody to the property set.