vsto +区分附件

时间:2012-09-04 02:07:16

标签: c# outlook vsto outlook-addin

我需要从邮件中获取并保存附件,但使用下面的代码会返回所有附件 - 这意味着它还会返回嵌入的图像,例如发件人的签名,徽标是图像。如何区分真实附件与嵌入图像?我在论坛上看到了很多,但我还不清楚。

public static void SaveData(MailItem currentMailItem)
{
    if (currentMailItem != null)
    {       
        if (currentMailItem.Attachments.Count > 0)
        {
            for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
            {
                currentMailItem.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
            }
        }
    }   
}

1 个答案:

答案 0 :(得分:8)

您可以使用以下pseudo-code from MS Technet Forums来检查附件是否内嵌。

if body format is plain text then
   no attachment is inline
else if body format is RTF then
   if PR_ATTACH_METHOD value is 6 (ATTACH_OLE) then
     attachment is inline
   else
     attachment is normal
else if body format is HTML then
   if PR_ATTACH_FLAGS value has the 4 bit set (ATT_MHTML_REF) then
     attachment is inline
   else
     attachment is normal

您可以使用MailItem.BodyFormat访问邮件正文格式,使用Attachment.PropertyAccessor访问 MIME附件属性

string PR_ATTACH_METHOD = 'http://schemas.microsoft.com/mapi/proptag/0x37050003';
var attachMethod = attachment.PropertyAccessor.Get(PR_ATTACH_METHOD);

string PR_ATTACH_FLAGS = 'http://schemas.microsoft.com/mapi/proptag/0x37140003';
var attachFlags = attachment.PropertyAccessor.Get(PR_ATTACH_FLAGS);