How to get email body, receipt, sender and CC info using EWS?

Steven Zack picture Steven Zack · Jul 12, 2011 · Viewed 48.8k times · Source

Can anyone tell me how to get an email body, receipt, sender, CC info using Exchange Web Service API? I only know how to get subject.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    service.Credentials = new NetworkCredential("user", "password", "domain");
    service.Url = new Uri("https://208.243.49.20/ews/exchange.asmx");
    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
    FindItemsResults<Item> findResults = service.FindItems(
        WellKnownFolderName.Inbox,
        new ItemView(10));

    foreach (Item item in findResults.Items)
    {
        div_email.InnerHtml += item.Subject+"<br />";
    }

My development environment is asp.net c# Exchange-server 2010 Thank you.

Answer

VeeTheSecond picture VeeTheSecond · Feb 14, 2013

Since the original question specifically asked for "email body, receipt, sender and CC info," I thought I would address those. I assume "receipt" is recipient info, and not the "notify sender" feature of email that no one uses. CC looks like it is handled the same way as recipients.

I liked Henning's answer to reduce the function to two calls, but had a little bit of difficulty figuring out how to handle a PropertySet. Google search was not immediately clear on this, and I ended up using someone else's tutorial:

// Simplified mail item
public class MailItem
{
    public string From;
    public string[] Recipients;
    public string Subject;
    public string Body;
}

public MailItem[] GetUnreadMailFromInbox()
{
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));
    ServiceResponseCollection<GetItemResponse> items = 
        service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
    return 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(),
        };
    }).ToArray();
}