【问题标题】:Getting an attachment using EWS and C# failing with ServiceMethodException使用 EWS 和 C# 获取附件失败并出现 ServiceMethodException
【发布时间】:2015-05-29 03:37:36
【问题描述】:

所以我目前正在构建一个应用程序,它允许一组用户查看来自某个电子邮件地址的所有电子邮件。这一切正常。当我尝试获取附件时,我遇到了问题。

我对这个领域比较陌生,并使用了 Microsoft 找到的示例 here. 将其与下面的代码进行比较:

    protected internal override Stream GetAttachmentStreamFinal(MailAttachmentDetails attachment)
    {
        var response = m_service.GetAttachments(new[] { attachment.Id }, BodyType.Text, Enumerable.Empty<PropertyDefinitionBase>());
        if (response.OverallResult != ServiceResult.Success)
        {
            if (response.Count > 0)
            {
                var ex = new MailException(response[0].ErrorMessage);
                ex.Data.Add(response[0].ErrorCode, response[0].ErrorMessage);
                foreach (var ed in response[0].ErrorDetails)
                {
                    ex.Data.Add(ed.Key, ed.Value);
                }
                throw ex;
            }
            throw new MailException("Error occurred while fetching the attachment from the mail service.");
        }

        foreach (var attachmentResponse in response)
        {
            if (attachmentResponse.Attachment is FileAttachment)
            {
                var fa = attachmentResponse.Attachment as FileAttachment;
                var cs = new MemoryStream(fa.Content);
                fa.Load(cs);
                cs.Seek(0, SeekOrigin.Begin);
                return cs;
            }
        }
        return null;
    }

如您所见,两组代码非常相似。但是,当我逐步进入 attachmentResponse.Attachment is FileAttachment 行时,我会抛出此错误

尝试通过方法“Mail.ExchangeEmailService.GetAttachmentStreamFinal(Mail.MailAttachmentDetails)”访问方法“Microsoft.Exchange.WebServices.Data.GetAttachmentResponse.get_Attachment()”失败。

一切都正确传递,响应返回成功。

在单步执行我的代码时,我注意到 Attachment 显示为非公共成员。但由于这是封装在 Microsoft 的类中,我不确定为什么会这样或我能做什么?

【问题讨论】:

  • 您是从邮件应用程序(在 Outlook 内部运行)执行此操作吗?您链接的示例适用于邮件应用程序,比普通的 EWS 应用程序更复杂。
  • 否,它在 Web 应用程序中运行。这在客户端请求文件时使用
  • 我也遇到了同样的问题,虽然我使用了最新版本的程序集形式 nuget,但是当我从 MSI 安装后引用程序集时它可以工作。你有没有找到一些稳定的解决方案?

标签: c# exchange-server exchangewebservices ews-managed-api


【解决方案1】:

我只想扩展@Jason Johnstons 的答案。

由于某种原因,NuGet 中的 EWS 版本不正确。它会引发您遇到的错误。

解决方法是通过删除对 NuGet 包的引用

Uninstall-Package Microsoft.Exchange.WebServices

然后在此处下载并运行 MSI 文件

https://www.microsoft.com/en-us/download/details.aspx?id=42951

这会将您需要的 DLL 安装到

的默认位置
[ C:\Program Files\Microsoft\Exchange\Web Services\2.2 ]

然后简单地将它们复制到您的 lib 目录(或类似目录)并直接创建对 DLL 的引用。

信用:http://www.resolvinghere.com/sm/microsoftexchangewebservicesdatagetattachmentresponsegetattachment-failed.shtml

【讨论】:

  • MS 现在应该已经在 NuGet 源中修复了这个问题!截至 2017 年 4 月,仍需要这样做。
  • 这对我很有帮助
【解决方案2】:

正如已经提到的其他答案,Microsoft 的 nuget 包不是最新的。我也遇到了和OP一样的问题。

首先,我按照 Daniel - SDS Group 的回答解决了这个问题。但后来我从marklamley 找到了nuget 包Exchange.WebServices.Managed.Api。它是ews-managed-api GitHub 项目的当前版本 2.2.1.1。

【讨论】:

  • 这个 nuget 包对我有用。当我在 Visual Studio 中运行代码时,我能够以语法方式下载附件。但是,当我在 AWS 托管中部署相同的代码时,它给了我我们之前遇到的错误 - '尝试通过方法'Mail.ExchangeEmailService.GetAttachmentStreamFinal(Mail.MailAttachmentDetails)' 访问方法'Microsoft.Exchange.WebServices.Data。 GetAttachmentResponse.get_Attachment()' 失败。”
  • @BalajiBirajdar 我确实在 Azure 中托管我的应用程序,并且没有使用 AWS 的经验。但是在本地运行或在云中运行应该没有区别。我从来没有遇到过这样的问题。
【解决方案3】:

确保您拥有最新版本的 Microsoft.Exchange.WebServices.dll。旧版本在调用 GetAttachments 方法的特定重载时不会返回实际的附件数据。

【讨论】:

  • 我使用的是我从link 下载的 2.2 版,这是正确的吗?属性显示版本 15.0.0.0 和运行时版本 2.0.50727
  • 你最后是对的。我已经引用了它,但在我的构建中它仍然使用旧版本 14。
  • 确保您在 GAC 中注册了正确的版本。今天本可以为我节省几个小时...
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 2014-11-30
  • 2015-11-12
  • 2015-04-11
  • 2023-03-06
  • 2020-03-06
  • 2013-02-13
  • 1970-01-01
相关资源
最近更新 更多