Don't save embed image that contain into attachements (like signature image)

前端 未结 4 1661
一向
一向 2020-12-19 15:05

I work on a VSTO in c#. When I click on button I save attachment in a folder. My problem is : when I have a rich email with an image in the signature, I have a element in my

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 15:51

    seeing that this question has some +2k hits and is still not answered, here is my attempt at a static utility method that returns a list of NON INLINE attachments:

    /// 
    /// Method to get all attachments that are NOT inline attachments (like images and stuff).
    /// 
    /// 
    /// The mail item.
    /// 
    /// 
    /// The .
    /// 
    public static List GetMailAttachments(Outlook.MailItem mailItem) {
        const string PR_ATTACH_METHOD = "http://schemas.microsoft.com/mapi/proptag/0x37050003";
        const string PR_ATTACH_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x37140003";
    
        var attachments = new List();
    
        // if this is a plain text email, every attachment is a non-inline attachment
        if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain && mailItem.Attachments.Count > 0) {
            attachments.AddRange(
                mailItem.Attachments.Cast().Select(attachment => attachment as Outlook.Attachment));
            return attachments;
        }
    
        // if the body format is RTF ...
        if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText) {
            // add every attachment where the PR_ATTACH_METHOD property is NOT 6 (ATTACH_OLE)
            attachments.AddRange(
                mailItem.Attachments.Cast().Select(attachment => attachment as Outlook.Attachment).Where(thisAttachment => (int)thisAttachment.PropertyAccessor.GetProperty(PR_ATTACH_METHOD) != 6));
        }
    
        // if the body format is HTML ...
        if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) {
            // add every attachment where the ATT_MHTML_REF property is NOT 4 (ATT_MHTML_REF)
            attachments.AddRange(
                mailItem.Attachments.Cast().Select(attachment => attachment as Outlook.Attachment).Where(thisAttachment => (int)thisAttachment.PropertyAccessor.GetProperty(PR_ATTACH_FLAGS) != 4));
        }
    
        return attachments;
    }
    
        

    提交回复
    热议问题