在遍历MailItem的Attachments集合的时候发现,不管是真正的附件还是内嵌资源,比如邮件内容中内嵌的图片(Embedded Image),都是Attachments集合的元素,通过查看attachment元素的属性,并没有发现可以区分它们的方法。
其实如果是Outlook2007及以上的话,可以通过MAPI Attachment Reference for PropertyAcessor取得attachment的ContentID来判断。
比较靠谱的判断方法是,
1)先看attachment的Type属性是不是OlAttachmentType.olByValue,如果不是那么它是内嵌的
2)再通过PropertyAccessor.GetProperty的方法看ContentID(http://schemas.microsoft.com/mapi/proptag/0x3712001E)和ContentLocation(http://schemas.microsoft.com/mapi/proptag/0x3713001E)是不是空的,如果是不为空的字符串,那么它是内嵌的
通常做1)和2)的check就行了,但某些情况下,这样还不保险,可以继续下面的check
3)通过PropertyAccessor.GetProperty的方法看METHOD属性(http://schemas.microsoft.com/mapi/proptag/0x37050003)的值是不是6,类型应该是int,如果是那么它是内嵌的
4)通过PropertyAccessor.GetProperty的方法看FLAGS属性(http://schemas.microsoft.com/mapi/proptag/0x37140003)的值是不是4,类型应该是int,如果是那么它是内嵌的
下面是代码判断:
[1]Subject: 物流平台 附件:image001.gif [olAttachment][olByValue] image002.gif [olAttachment][olByValue] 翘运工程项目集中采购二次加工之物流控制平台.docx [olAttachment][olByValue] 邮件内容:<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmln private bool isEmbeddedAttachment(Outlook.Attachment attachment) { if(attachment.Type != Outlook.OlAttachmentType.olByValue) { return true; } string ATTACH_CONTENT_ID =@"http://schemas.microsoft.com/mapi/proptag/0x3712001E"; string ATTACH_CONTENT_LOCATION = @"http://schemas.microsoft.com/mapi/proptag/0x3713001E"; if(attachment.PropertyAccessor.GetProperty(ATTACH_CONTENT_ID).ToString() != string.Empty || attachment.PropertyAccessor.GetProperty(ATTACH_CONTENT_LOCATION).ToString()!= string.Empty) { return true; } string ATTACH_METHOD =@"http://schemas.microsoft.com/mapi/proptag/0x37050003"; if((int)attachment.PropertyAccessor.GetProperty(ATTACH_METHOD) == 6) { return true; } string ATTACH_FLAGS = @"http://schemas.microsoft.com/mapi/proptag/0x37140003"; if((int)attachment.PropertyAccessor.GetProperty(ATTACH_FLAGS) == 4) { return true; } return false; }
int count = item.Attachments.Count; //删除邮件中的附件,保留内嵌资源 for(int i = count; i > 0; i--) { if(!isEmbeddedAttachment(item.Attachments[i])) { item.Attachments[i].Delete(); } }
(这个方法可能还有点问题,如果附件中包含压缩文件(例如.zip文件)通过这个方法判断,也会被当成是内嵌资源。)
转:http://jianyun.org/archives/775.html
[VSTO] 区分MAILITEM的ATTACHMENT是真正的附件还是内嵌资源
原文:https://www.cnblogs.com/q149072205/p/11347533.html