【问题标题】:vsto + cannot get the filename of the attachment from a RTF emailvsto + 无法从 RTF 电子邮件中获取附件的文件名
【发布时间】:2022-04-03 01:31:30
【问题描述】:

我正在保存邮件中的附件。我首先检查正文格式,以便我只得到真正的附件。请忽略下面的 if else 语句,只有 cmets 作为下一个语句。一旦我解决了我的问题,我会编写代码。现在,我的问题是在获取 RichText 正文格式邮件附件的文件名时出现此错误。一切都很好,都是纯 HTML 格式。

“currentMailItem.Attachments[1].FileName”引发了“System.Runtime.InteropServices.COMException”类型的异常 base {System.Runtime.InteropServices.ExternalException}:{“Outlook 无法对此类附件执行此操作。”}

public static void SaveData(MailItem currentMailItem)
{
    if (currentMailItem != null)
    {       
        string PR_ATTACH_METHOD = "http://schemas.microsoft.com/mapi/proptag/0x37050003";
        string PR_ATTACH_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x37140003";

        if (currentMailItem.Attachments.Count > 0)
        {
            for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
            {
                var attachMethod = currentMailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_METHOD);
                var attachFlags = currentMailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_FLAGS);

                if (currentMailItem.BodyFormat == OlBodyFormat.olFormatPlain)
                    //no attachment is inline
                else if (currentMailItem.BodyFormat == OlBodyFormat.olFormatRichText)
                {
                    if (attachMethod == 6)
                        //attachment is inline
                    else
                        //attachment is normal
                }
                else if (currentMailItem.BodyFormat == OlBodyFormat.olFormatHTML)
                {
                    if (attachFlags == 4)
                        //attachment is inline
                    else
                        //attachment is normal
                }
                currentMailItem.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
            }
        }
    }   
}

【问题讨论】:

  • 你能提供一个我们可以查看的示例味精吗? Attachment.Type 是什么?

标签: c# outlook vsto outlook-addin


【解决方案1】:

对于 RTF 格式,您嵌入了 RTF 正文使用的 OLE 对象,以显示附件预览并就地编辑它。您可以按附件类型 (Attachment.Type == olAttachmentType.olOLE (6)) 过滤掉此类附件,也可以使用任一方法提取附件数据

  1. 扩展 MAPI(仅限 C++ 或 Delphi)- 调用 IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IStorage, ...) 以将附件数据打开为 IStorage,然后从 IStorage 流之一中提取文件数据。确切的流名称和格式取决于用于创建它的特定应用程序(Word、Adobe、Paintbrush、Excel 等)。您可以在OutlookSpy 中看到数据(我是它的作者):选择带有类似附件的邮件,单击 OutlookSpy 功能区上的 IMessage 按钮,转到 GetAttachmentTable 选项卡,双击附件,选择 PR_ATTACH_DATA_OBJ 属性,右键单击,选择 OpenProperty,选择 IID_IStorage。

  2. Redemption(任何语言 - 我也是它的作者):它的 RDOAttachment 对象足够智能,可以在您调用 SaveAsFile 或访问 FileName 属性时提取 OLE 附件的实际文件数据。

【讨论】:

    【解决方案2】:

    这可能是老问题...

    我见过类似的问题。如果图像附加在富文本格式的 mailItem 的正文中,那么我会得到类似的异常。

    但是,如果附件不是图像,请说 .doc 扩展名,然后我看不到错误。据我了解,问题在于获取附加图像的文件名。一些 Outlook 不知道如何从富文本格式的电子邮件项目中获取正文附加图像的文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-24
      • 2011-08-20
      • 2011-09-19
      • 2020-11-06
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2014-07-15
      相关资源
      最近更新 更多