2011-05-17 105 views
1

我已經制定了下面的代碼,它的工作原理:如何從EWS的傳入電子郵件中獲得UniqueBody?

private void OnEvent(object sender, NotificationEventArgs args) 
{ 
    StreamingSubscription sub = args.Subscription; 

    foreach (NotificationEvent notification in args.Events) 
    { 
     switch (notification.EventType) 
     { 
      case EventType.NewMail: 
       if (notification is ItemEvent) 
       { 
        ItemEvent item = (ItemEvent)notification; 
        EmailMessage message = EmailMessage.Bind(service, item.ItemId); 

        string fAddress = message.From.Address; 
        string subject = message.Subject; 
        string body = message.Body.Text; 
        string tAddress = message.ToRecipients[0].Address; 

        //and so on... 
       } 
       break; 
     } 
    } 
} 

但是,如果我嘗試設置「身體」等於UniqueBody像這樣...

string body = message.UniqueBody.Text; 

的錯誤說出來,「您必須在讀取其值之前加載或分配此屬性。」我希望UniqueBody能夠開箱即用,這意味着我不需要解析一封新郵件來獲取我關心的新細節。我假設有一些我很想念的東西。有任何想法嗎?

回答

4

當你綁定ItemId你希望收到你需要明確你想要的屬性。

例如,

var propertySet = new PropertySet(ItemSchema.UniqueBody); 
var email = EmailMessage.Bind(service, item.ItemId, propertySet); 

PropertySet類具有包括params[]所以你可以自由地包括一個過載/排除多個附加屬性。只需查看ItemSchema枚舉並選擇你想要的。

1

這是我最終使用:

PropertySet pSet = new PropertySet(new[]{ ItemSchema.UniqueBody }); 
pSet.RequestedBodyType = BodyType.Text; 
pSet.BasePropertySet = BasePropertySet.FirstClassProperties; 
相關問題