C# Exchange 服务突然报错 The 属性 Hashtags is valid only for Exchange Exchange2015 or later versions

C# Exchange Service suddenly giving error The property Hashtags is valid only for Exchange Exchange2015 or later versions

我有一个用 C# 编写的 Windows 服务,它轮询 Exchange 服务器以处理发送到无人值守邮箱的邮件。

它一直运行良好,直到今天抛出以下错误:-

EXCEPTION: Microsoft.Exchange.WebServices.Data.ServiceVersionException: The property Hashtags is valid only for Exchange Exchange2015 or later versions.
   at Microsoft.Exchange.WebServices.Data.PropertyBag.set_Item(PropertyDefinition propertyDefinition, Object value)
   at Microsoft.Exchange.WebServices.Data.ComplexPropertyDefinitionBase.InternalLoadFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag)
   at Microsoft.Exchange.WebServices.Data.ComplexPropertyDefinitionBase.LoadPropertyValueFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag)
   at Microsoft.Exchange.WebServices.Data.PropertyBag.LoadFromXml(EwsServiceXmlReader reader, Boolean clear, PropertySet requestedPropertySet, Boolean onlySummaryPropertiesRequested)
   at Microsoft.Exchange.WebServices.Data.ServiceObject.LoadFromXml(EwsServiceXmlReader reader, Boolean clearPropertyBag, PropertySet requestedPropertySet, Boolean summaryPropertiesOnly)
   at Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader.ReadServiceObjectsCollectionFromXml[TServiceObject](XmlNamespace collectionXmlNamespace, String collectionXmlElementName, GetObjectInstanceDelegate`1 getObjectInstanceDelegate, Boolean clearPropertyBag, PropertySet requestedPropertySet, Boolean summaryPropertiesOnly)
   at Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader.ReadServiceObjectsCollectionFromXml[TServiceObject](String collectionXmlElementName, GetObjectInstanceDelegate`1 getObjectInstanceDelegate, Boolean clearPropertyBag, PropertySet requestedPropertySet, Boolean summaryPropertiesOnly)
   at Microsoft.Exchange.WebServices.Data.GetItemResponse.ReadElementsFromXml(EwsServiceXmlReader reader)
   at Microsoft.Exchange.WebServices.Data.ServiceResponse.LoadFromXml(EwsServiceXmlReader reader, String xmlElementName)
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.ParseResponse(EwsServiceXmlReader reader)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ParseResponse(EwsServiceXmlReader reader, WebHeaderCollection responseHeaders)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadResponse(EwsServiceXmlReader ewsXmlReader, WebHeaderCollection responseHeaders)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponseXml(Stream responseStream, WebHeaderCollection responseHeaders)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponse(IEwsHttpWebResponse response)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalLoadPropertiesForItems(IEnumerable`1 items, PropertySet propertySet, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.LoadPropertiesForItems(IEnumerable`1 items, PropertySet propertySet)
   at MyServiceName.MyServiceName.CheckForNewEmails(ExchangeService service) in C:\TFSOnline\RPM Tools\MyServiceName\MyServiceName\MyServiceName.cs:line 177
   at MyServiceName.MyServiceName.RunACheck(Object state) in C:\TFSOnline\RPM Tools\MyServiceName\MyServiceName\MyServiceName.cs:line 117

导致崩溃的行是:

PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);

具体错误为:

Microsoft.Exchange.WebServices.Data.ServiceVersionException: 
The property Hashtags is valid only for Exchange Exchange2015 or later versions.

我没有在任何地方使用或访问主题标签,所以大概它包含在 BasePropertySet.FirstClassProperties 中。

正如我提到的,到目前为止,我一直很高兴地使用上面的代码,今天才开始出现这个错误。

我正在使用最新的 EWS 稳定版本:Exchange.WebServices.Managed.Api 2.2.1.1

BasePropertySet 只有一项,IdOnly,而我要找的 属性 是 InternetMessageId,所以我想我应该使用它:

PropertySet properties = (BasePropertySet.IdOnly);
service.LoadPropertiesForItems(emails, properties);

原来 InternetMessageId 没有被 属性 覆盖以获取 ID...

EXCEPTION: Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException: 
You must load or assign this property before you can read its value.
   at Microsoft.Exchange.WebServices.Data.PropertyBag.get_Item(PropertyDefinition propertyDefinition)
   at Microsoft.Exchange.WebServices.Data.EmailMessage.get_InternetMessageId()

给出错误的代码部分没有改变,据我所知,没有影响 Exchange 的 Office 365 更新。

为了仔细检查,我使用了以前的版本(编译代码,而不是我重新编译的源代码)并安装了它:仍然出现相同的错误。

以前有人遇到过这个错误吗?我已经用谷歌搜索了,但没有得到任何结果。

编辑:尝试在另一台机器上安装该服务,如果它特定于它所在的机器。

即使使用之前的代码,仍然会出现相同的错误。

解决了。原来是特定电子邮件中的内容。

最初代码的顺序是:-

PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);

foreach (var email in emails)
{
    try {
        <Process Email>
    }
}

我将代码重新排序为:-

foreach (var email in emails)
{
    try {
        var list = new List<EmailMessage> { email };
        PropertySet properties = (BasePropertySet.FirstClassProperties);
        service.LoadPropertiesForItems(list, properties);
        <Process Email>
    }
} 

因此,为每封电子邮件单独获取属性,而不是一起获取属性。必须通过列表完成,因为这就是 LoadPropertiesForItems 所需要的。

在这种情况下,其中一封电子邮件包含导致错误的内容(大概是标签 属性)。

现在所有其余的电子邮件都得到了正确处理,导致错误的那封被跳过。

虽然很烦人,但因为我无法访问这些属性,所以我实际上无法记录有关它的任何信息。

尽量不要使用 PropertySet properties = (BasePropertySet.FirstClassProperties);,因为它可能会 return 字段超过您的需要。 (超过 50 个属性)

尝试加载您需要的。

PropertySet ps = new PropertySet(
                                            ItemSchema.Subject,
                                            ItemSchema.InReplyTo,
                                            ItemSchema.Body,
                                            ItemSchema.DateTimeSent,
                                            ItemSchema.DisplayTo,
                                            ItemSchema.Importance,
                                            EmailMessageSchema.From,
                                            ItemSchema.UniqueBody, 
                                            ItemSchema.MimeContent, 
                                            ItemSchema.HasAttachments, 
                                            ItemSchema.Attachments
                                            );

您也可以尝试将 ExchangeService 构造函数中的 requestedServerVersion 显式设置为更高的值。如果这涉及Office365,请记住EWS is not receiving further updates.

您也可以尝试将 ExchangeService 构造函数中的 requestedServerVersion 显式设置为更高的值。对于 Office365,请记住 EWS is not receiving further updates.

我遇到了同样的问题。我注意到你和我都没有指定我们使用的是哪个交换服务器。这对我有用了很长时间,但最终我得到了这个错误。指定哪个交换服务器为我解决了这个问题。

这段代码对我有用。我需要添加“新 PropertySet”才能使其正常工作。

foreach (var email in emails)
{
    try {
        var list = new List<EmailMessage> { email };
        PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);
        service.LoadPropertiesForItems(list, properties);
        <Process Email>
    }
}

但最终使它起作用的是将 Exchange 版本从 Exchange2013 设置为 Exchange2015:

旧代码:

    service = new ExchangeService(ExchangeVersion.Exchange2013)
    {
        Credentials = new WebCredentials(user, password),
        TraceEnabled = false,
        TraceFlags = TraceFlags.None
    };

新代码:

    service = new ExchangeService(ExchangeVersion.Exchange2015)
    {
        Credentials = new WebCredentials(user, password),
        TraceEnabled = false,
        TraceFlags = TraceFlags.None
    };