在遍历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/0×37050003)的值是不是6,类型应该是int,如果是那么它是内嵌的
4)通过PropertyAccessor.GetProperty的方法看FLAGS属性(http://schemas.microsoft.com/mapi/proptag/0×37140003)的值是不是4,类型应该是int,如果是那么它是内嵌的
下面是代码判断:
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();
}
}
原创文章,转载请注明: 转载自闲云博客
留下评论







1 Comment(s)