2010-10-07 35 views
4

我在C#中使用VSTO。当我点击按钮时,我将附件保存在一个文件夹中。我的问题是:当我在签名中有一张带有图像的丰富电子邮件时,我的附件中有一个元素。但我不想保存那张图片。 Outlook(应用程序)将该附件隐藏在区域附件中!那么,为什么不是我:-(不保存包含到附件中的嵌入图像(如签名图像)

我的代码是很干脆:

MailItem MailItemSelected = this.OutlookItem; 
foreach (Attachment a in MailItemSelected.Attachments) 
{ 
    a.SaveAsFile(path + a.FileName); 
} 

但我没有找到一个测试为不保存签名的图像

回答

2

我的一切。发现我的解决方案的一个组成部分。 当你创建一个电子邮件图像嵌入的尺寸是0,所以可以排除这一点。

但是,当我读了电子邮件这是不对的。

MailItem MailItemSelected = this.OutlookItem; 
foreach (Attachment a in MailItemSelected.Attachments) 
{          
    if(a.Size != 0) 
     a.SaveAsFile(path + a.FileName); 
} 

当我读电子邮件时,我发现了一个解决方案,但它不是很好。所以我写了,但如果有人认为更好,我喜欢它。 在我的示例中,我尝试使用PropertyAccessor获取Flag属性,如果它是嵌入图像,那么还可以,我有一个会引发的异常。

MailItem MailItemSelected = this.OutlookItem; 
foreach (Attachment a in MailItemSelected.Attachments) 
{ 
    bool addAttachment = false; 
    try 
    { 
     string schemaPR_ATTACH_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x37140003"; 
     a.PropertyAccessor.GetProperty(schemaPR_ATTACH_FLAGS); 
    } 
    catch 
    { 
     addAttachment = true; 
    } 

    if (addAttachment && (a.Size != 0)) 
     a.SaveAsFile(path + a.FileName); 
} 
2

看到,这个问题有一定的+ 2K安打,依然没有回答,这是我在一个静态实用方法试图返回NON INLINE附件列表:

/// <summary> 
/// Method to get all attachments that are NOT inline attachments (like images and stuff). 
/// </summary> 
/// <param name="mailItem"> 
/// The mail item. 
/// </param> 
/// <returns> 
/// The <see cref="List"/>. 
/// </returns> 
public static List<Outlook.Attachment> 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<Outlook.Attachment>(); 

    // 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<object>().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<object>().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<object>().Select(attachment => attachment as Outlook.Attachment).Where(thisAttachment => (int)thisAttachment.PropertyAccessor.GetProperty(PR_ATTACH_FLAGS) != 4)); 
    } 

    return attachments; 
} 
+0

如果您有兴趣,请提交此:HTTP://stackoverflow.com/documentation/outlook-addin/commit – 2016-07-28 04:35:40

4

我们有一个需要在Outlook加载项中只显示“邮件附件”(不是用于渲染的嵌入的附件),这就是工作原理。

if (mailItem.Attachments.Count > 0) 
     { 
      // get attachments 
      foreach (Attachment attachment in mailItem.Attachments) 
      { 
       var flags = attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003"); 

       //To ignore embedded attachments - 
       if (flags != 4) 
       { 
        // As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachment then Type value is 6 and ignore it 
        if ((int)attachment.Type != 6) 
        { 

         MailAttachment mailAttachment = new MailAttachment { Name = attachment.FileName }; 
         mail.Attachments.Add(mailAttachment); 
        } 

       } 

      } 
     } 
0

标志解决方法对我无效。我正在开发用于Outlook 2016年的解决方案,我设法过滤使用此代码附件:

foreach (Attachment attachment in mailItem.Attachments) 
      { 
       //exclude inline images 
       if (!mailItem.HTMLBody.Contains(attachment.FileName)) 
       { 
        //the attachment is not an inline attachment, YOUR CODE HERE 
       } 
    } 

如果每个附件,并在标签中提到的HTML体,其基本检查。


编辑:如果您在正文中键入它的名字前面的方法可能会跳过的附件。这是不太LIKEY跳过误报

if (!mailItem.HTMLBody.Contains("cid:" + attachment.FileName)) 
+1

大多数图像在HTML由内容称id(cid)。您需要同时查看文件名和内容标识(PR_ATTACH_CONTENT_ID MAPI属性 - 使用OutlookSpy查看该消息)。您的代码将不适用于内容ID。如果还会错误地标记邮件正文中提到的附件(例如“我将TheFile.doc附加到您的评论中”) – 2017-04-19 20:08:06

相关问题