• [VSTO] 区分MAILITEM的ATTACHMENT是真正的附件还是内嵌资源


    在遍历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

  • 相关阅读:
    angular中scope的watch用法
    angular中对于no-repeat的优化——track by
    angular筛选器
    为什么我只贴代码不给你们源码?
    新添子节点却无法被之前的删除功能所删除,处理方法,给删除功能延迟,有好办法记得告诉我下哈,感激不进
    IDEA下Maven项目搭建踩坑记----3.最长的bug,最简单的错误。同一类中,部分函数的@AutoWired注入的对象失效
    IDEA下Maven项目搭建踩坑记----2.项目编译之后 在service层运行时找不到 com.dao.CarDao
    IDEA下Maven项目搭建踩坑记----1.pom,xml文件下${spring-version}不能用
    Myeclipse maven 配置有问题 改之后重启还是不好用
    前端 的一些css的写法
  • 原文地址:https://www.cnblogs.com/q149072205/p/11347533.html
Copyright © 2020-2023  润新知