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

本文关键字:电子邮件 获取 UniqueBody EWS | 更新日期: 2023-09-27 17:56:38

我有以下代码,它可以工作:

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能够开箱即用,这意味着我不必解析新电子邮件来获取我关心的新细节。 我假设我错过了一些简单的东西。 有什么想法吗?

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

当你绑定你想要接收的ItemId时,你需要明确你想要的属性。

例如

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

PropertySet类具有包含params[]的重载,因此您可以自由包含/排除许多其他属性。只需浏览ItemSchema枚举并选择所需的枚举即可。

这是我最终使用的:

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