Skip to main content
 首页 » 编程设计

c#之如何从 EWS 中的传入电子邮件中获取 UniqueBody

2024年02月03日31mq0036

我有以下代码,并且有效:

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

但是,如果我像这样尝试将“body”设置为等于 UniqueBody...

string body = message.UniqueBody.Text; 

错误的说法是,“您必须先加载或分配此属性,然后才能读取它的值。”我希望 UniqueBody 能够开箱即用,这意味着我不必解析新电子邮件来获取我关心的新细节。我假设我缺少一些简单的东西。有什么想法吗?

请您参考如下方法:

当您绑定(bind)希望收到的 ItemId 时,您需要明确说明您想要的属性。

例如,

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

PropertySet 类有一个包含 params[] 的重载,因此您可以自由地包含/排除一些额外的属性。只需浏览 ItemSchema 枚举并选择您想要的。