8

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

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);
            }
        }
    }   
}
4

1 回答 1

8

您可以使用MS Technet 论坛中的以下伪代码检查附件是否内联。

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,并使用访问MIME 附件属性Attachment.PropertyAccessor

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);
于 2012-09-04T15:27:42.760 回答