vsto +无法从RTF电子邮件中获取附件的文件名

时间:2012-09-05 09:33:17

标签: c# outlook vsto outlook-addin

我正在保存邮件中的附件。我首先检查身体格式,以便我只获得真正的附件。请忽略下面的if else语句,仅将注释作为下一个语句。一旦我解决了问题,我就会编码。现在,我的问题是我在获取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);
            }
        }
    }   
}

2 个答案:

答案 0 :(得分:2)

对于RTF格式,您已嵌入了RTF正文使用的OLE对象,以显示附件预览并进行就地编辑。您可以通过附件类型(Attachment.Type == olAttachmentType.olOLE(6))筛选出此类附件,也可以使用

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

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

答案 1 :(得分:0)

这可能是个老问题......

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

但是,如果附件不是图片,比如.doc扩展名,那么我就不会看到错误。据我所知,问题在于获取附加图像的文件名。一些Outlook如何不知道如何从富文本格式的电子邮件项中获取正文附加图像的文件名。

相关问题