如何使用EWS获取电子邮件正文,收据,发件人和CC信息?

时间:2011-07-12 19:18:24

标签: c# asp.net exchange-server exchangewebservices

有人能告诉我如何使用Exchange Web服务API获取电子邮件正文,收据,发件人,CC信息吗?我只知道如何获得主题。

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 />";
    }

我的开发环境是asp.net c#Exchange-server 2010 谢谢。

4 个答案:

答案 0 :(得分:33)

由于原始问题特别要求“电子邮件正文,收据,发件人和CC信息”,我想我会解决这些问题。我假设“收据”是收件人信息,而不是没有人使用的电子邮件的“通知发件人”功能。 CC看起来像收件人一样处理。

我喜欢Henning的答案,将函数减少为两个调用,但在确定如何处理PropertySet时遇到了一些困难。谷歌搜索没有立即清楚,我最终使用了别人的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();
}

答案 1 :(得分:10)

使用FindItems只会让你到目前为止,因为它只返回正文的前255个字节。您应该做的是FindItem组合请求邮件的ID并发出一个或多个GetItem来调用以获取您感兴趣的属性。

答案 2 :(得分:2)

在这里你会找到解决方案。

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

    }

答案 3 :(得分:2)

您也可以转换为EmailMessage并直接读取所需的属性,而不是使用ExtendedProperties。例如发件人地址:

((Microsoft.Exchange.WebServices.Data.EmailMessage)(item)).From.Address;