2011-05-17 107 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; 
相关问题