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

独自空忆成欢 提交于 2019-12-02 18:06:19
Henning Krause

Using FindItems will only get you so far, because it does only return the first 255 bytes of a body. What you should do is a combination of FindItem to request the ids of the mails and issue one or more GetItem calls to get the properties you are interested in.

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

here you will find the solution.

http://blogs.msdn.com/b/akashb/archive/2010/03/05/how-to-build-a-complex-search-using-searchfilter-and-searchfiltercollection-in-ews-managed-api-1-0.aspx


 // Send the request to search the Inbox and get the results.
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, FinalsearchFilter, view);



        // Process each item.
        if (findResults.Items.Count > 0)
        {
            foreach (Item myItem in findResults.Items)
            {
                if (myItem is EmailMessage)
                {
                    Console.WriteLine((myItem as EmailMessage).Subject);
                }
                if (myItem.ExtendedProperties.Count > 0)
                {
                    // Display the extended property's name and property.
                    foreach (ExtendedProperty extendedProperty in myItem.ExtendedProperties)
                    {
                        Console.WriteLine(" Extended Property Name: " + extendedProperty.PropertyDefinition.Name);
                        Console.WriteLine(" Extended Property Value: " + extendedProperty.Value);
                    }
                }

            }
        }
        else
        {
            Console.WriteLine("No Items Found!");
        }

    }

Instead of using the ExtendedProperties, you could also cast to EmailMessage and read the property you want directly. For example the sender address:

((Microsoft.Exchange.WebServices.Data.EmailMessage)(item)).From.Address;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!