【问题标题】:Handling illegal XML values while reading emails with EWS使用 EWS 阅读电子邮件时处理非法 XML 值
【发布时间】:2011-10-14 05:12:45
【问题描述】:

我们有一个应用程序使用StreamingSubscriptionConnection 来阅读发送到特定邮箱的每封 封电子邮件。我在开发过程中每天都会遇到几次问题,我得到了异常{"'{square character}', hexadecimal value 0x1F, is an invalid character. Line 1, position 1."}

这是堆栈跟踪:

   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
   at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
   at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Int32 pos, Char invChar)
   at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
   at System.Xml.XmlTextReaderImpl.ParseText()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read()
   at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read(XmlNodeType nodeType)
   at Microsoft.Exchange.WebServices.Data.EwsXmlReader.InternalReadElement(XmlNamespace xmlNamespace, String localName, XmlNodeType nodeType)
   at Microsoft.Exchange.WebServices.Data.EwsXmlReader.ReadStartElement(XmlNamespace xmlNamespace, String localName)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadResponse(EwsServiceXmlReader ewsXmlReader)

如何使用 EWS 安全阅读包含非法字符的电子邮件?

经过大量搜索,似乎可以使用旧版本的 EWS API 解决此问题。但是,使用最新版本的托管 API 似乎没有人能解决问题。

这是来自http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/22863099-1d93-47ac-a11b-08c6bf7facea 的交叉帖子。

我已经设法再次获得异常,这里是完整的堆栈跟踪以及 Exchange 正在作为通知的内容。

我使用的是 Exchange 2010 SP1。

编辑:我正在重新提出这个问题,因为它给我带来了严重的问题,而原始问题清楚地说明了问题。我正在寻找修改托管 EWS API 的行为以过滤 XML 中的无效字符并避免异常的客户端解决方案。 Exchange 服务器修复不太可能成为一种选择,除非它们是简单的配置更改。我的软件将在我无法控制的客户 Exchange 服务器上运行。

【问题讨论】:

标签: c# xml exchangewebservices


【解决方案1】:

一般的解决方法是编写一个实用程序,将收件箱文件夹作为后台进程进行监视,如果您无法解决接收无效 XML 文件的问题

该程序将执行以下操作:

您应该能够使用这样的转义序列安全地替换US-ASCII 32 以下的所有non-printable 字符,但“\r”、“\n”和“\t”除外。但是,您还必须确保不会损坏 XML 文件,并且无论系统使用它们,更改的 XML 文件仍然可以使用。

或者寻找更常见的 XML sanitizer 库之一。

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2019-02-23
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多