【问题标题】:How to save attached EmailMessage from ItemAttachment object in Exchange EWS?如何从 Exchange EWS 中的 ItemAttachment 对象中保存附加的 EmailMessage?
【发布时间】:2016-03-11 22:41:30
【问题描述】:

我一直在与这个问题作斗争一段时间,但未能在网上找到有效的答案。我正在使用 Exchange EWS API 进行一些电子邮件处理。我需要处理的一件事是带有附件的 EmailMessage。其中一个附件恰好是另一个 EmailMessage。我将其称为附加的 EmailMessage。

我想将此 EmailMessage 转换为 byte[],但是每次尝试时,我都会收到异常。以下是我的代码:

if (((ItemAttachment)attachment).Item is EmailMessage)
{
     EmailMessage msg = ((ItemAttachment)attachment).Item as EmailMessage;
     msg.Load(new PropertySet(ItemSchema.MimeContent)); 
     byte[] content = msg.MimeContent.Content; 
}

问题是无论我尝试加载什么,都会抛出异常提示

Microsoft.Exchange.WebServices.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:附件不支持此操作。

如果我不调用 msg.Load(),我会收到一个不同的错误,说我需要加载内容。

我不明白这一点。如果我对未附加任何内容的 EmailMessage 执行相同的操作,它就可以正常工作。为什么 EmailMessage 在某个时间点是附件很重要?如何让 EWS/.NET/Whatever 抛出异常以将附加的 EmailMessage 视为 EmailMessage 而不是 ItemAttachment?

【问题讨论】:

    标签: c# exchangewebservices


    【解决方案1】:

    您需要使用包含 MimeContent 的属性集的嵌入式附件上的 Each 上的 GetAttachments 操作。例如类似的东西(如果您处理多条消息等,通过对 GetAttachment 请求进行分组可以提高效率)。

                PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
            psPropSet.Add(ItemSchema.MimeContent);
            foreach (Attachment attachment in CurrentMessage.Attachments)
            {
                if (attachment is ItemAttachment)
                {
                    attachment.Load();
                    if (((ItemAttachment)attachment).Item is EmailMessage)
                    {
                        EmailMessage ebMessage = ((ItemAttachment)attachment).Item as EmailMessage;
                        foreach (Attachment ebAttachment in ebMessage.Attachments)
                        {
                            if (ebAttachment is ItemAttachment)
                            {
                                Attachment[] LoadAttachments = new Attachment[1];
                                LoadAttachments[0] = ebAttachment;
                                ServiceResponseCollection<GetAttachmentResponse> getAttachmentresps = service.GetAttachments(LoadAttachments, BodyType.HTML, psPropSet);
                                foreach (GetAttachmentResponse grResp in getAttachmentresps)
                                {
                                    EmailMessage msg = ((ItemAttachment)grResp.Attachment).Item as EmailMessage;
                                    msg.Load(new PropertySet(ItemSchema.MimeContent));
                                    byte[] content = msg.MimeContent.Content;
                                }
                            }
                        }
    
                    }
                }
            }
    

    【讨论】:

    • 嗨,格伦,感谢您的回复。我正在尝试实现它,但遇到了一些障碍。我正在引用 Microsoft.Exchange.WebServices.dll,但出现“GetAttachmentResponse”的编译器错误,并且我的 ExchangeService 对象没有名为 GetAttachments(...) 的方法 我没有 Visual Studio 解决的选项任何一个。我需要参考另一个程序集吗? - 谢谢。
    • 无视。我有一个旧的 .dll 版本。
    【解决方案2】:

    您必须加载带有标志的附件才能加载 MimeContent:

    {

    if (attachment is ItemAttachment ia)
            {
                ia.Load(ItemSchema.MimeContent);
            }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 2017-03-15
      • 2011-11-22
      • 2014-05-04
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多