NetOffice.Outlook如何区分电子邮件正文中的图像和附件中的图像?

huangapple go评论54阅读模式
英文:

NetOffice.Outlook distinguish between the images in the body of the email and the attached ones?

问题

如何区分电子邮件正文中的图片和附加到电子邮件的图片,使用NetOffice.Outlook C#库?

更新:

这是我的半工作示例:

var saveInlineImages = false;
foreach (var attachment in mailItem.Attachments)
{
    if (attachment == null) continue;
    using (attachment)
    {
        var attName = attachment.FileName;
        var savePath = GetSavePath();
        try
        {
            if (!saveInlineImages && IsImage(attName) && IsInlineImage(attachment))
                continue;
            attachment.SaveAsFile(savePath);
        }
        catch
        {
            throw new Exception($"Failed to write the file: {savePath}");
        }
    }

}

private bool IsImage(string attName)
{
    var lowerName = attName.ToLower();
    return lowerName.EndsWith(".jpg") || lowerName.EndsWith(".jpeg") ||
           lowerName.EndsWith(".png") || lowerName.EndsWith(".gif") ||
           lowerName.EndsWith(".tiff") || lowerName.EndsWith(".svg") ||
           lowerName.EndsWith(".raw") || lowerName.EndsWith(".ico") ||
           lowerName.EndsWith(".heic");
}

private bool IsInlineImage(Attachment attachment)
{
    try
    {
        var propertyAccessor = attachment.PropertyAccessor;
        var cid = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F");
        return !string.IsNullOrEmpty(cid?.ToString());
    }
    catch
    {
        return false;
    }
}

问题是,“IsInlineImage”在除Exchange以外的所有帐户上运行正常。在Exchange帐户中,此方法将所有消息定义为“内联”。是否有其他通用的工作方法来检测消息正文中的图片?

英文:

How can I distinguish between an image in the body of an email and an image attached to an email using the NetOffice.Outlook C# library?

UPD:

Here's my semi-working example:

var saveInlineImages = false;
foreach (var attachment in mailItem.Attachments)
{
    if (attachment == null) continue;
    using (attachment)
    {
        var attName = attachment.FileName;
        var savePath = GetSavePath();
        try
        {
            if (!saveInlineImages && IsImage(attName) && IsInlineImage(attachment))
                continue;
            attachment.SaveAsFile(savePath);
        }
        catch
        {
            throw new Exception($"Failed to write the file: {savePath}");
        }
    }

}

private bool IsImage(string attName)
{
    var lowerName = attName.ToLower();
    return lowerName.EndsWith(".jpg") || lowerName.EndsWith(".jpeg") ||
           lowerName.EndsWith(".png") || lowerName.EndsWith(".gif") ||
           lowerName.EndsWith(".tiff") || lowerName.EndsWith(".svg") ||
           lowerName.EndsWith(".raw") || lowerName.EndsWith(".ico") ||
           lowerName.EndsWith(".heic");
}

private bool IsInlineImage(Attachment attachment)
{
    try
    {
        var propertyAccessor = attachment.PropertyAccessor;
        var cid = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F");
        return !string.IsNullOrEmpty(cid?.ToString());
    }
    catch
    {
        return false;
    }
}

The problem is that "IsInlineImage" works fine on all accounts except Exchange. In Exchange accounts, this method defines all messages as "Inline". Is there any other universal working method to detect a picture inside the message body?

答案1

得分: 1

不管您是否使用NetOffice,您都可以通过检查src属性来区分消息正文中使用的图像。对于嵌入的图像,您可以看到以下构造:

<img src="cid:image.png"/>

其中cid:表示所提及的图像表示附加文件,并且在消息正文中作为图像使用。

您可以尝试使用PropertyAccessor对象获取附加文件上的PR_ATTACH_CONTENT_ID属性值(DASL名称为"http://schemas.microsoft.com/mapi/proptag/0x3712001F"),该值用于HTML标记中的cid属性。PropertyAccessor.GetProperty方法可以帮助处理此类任务。

英文:

It doesn't matter whether you use NetOffice or not, you can distinguish images used in the message body by checking the src attribute. In case embedded images you could see the following construction:

&lt;img src=&quot;cid:image.png&quot;/&gt;

Where the cid: indicates that the image mentioned represents an attached file and used in the message body as an image.

You may try getting the PR_ATTACH_CONTENT_ID property value on the attached file (the DASL name is "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using the PropertyAccessor object. The value is used for the cid attribute in the HTML markup. The PropertyAccessor.GetProperty method can help with such tasks.

huangapple
  • 本文由 发表于 2023年7月6日 14:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定