我正在尝试找到一种解决方案,以区分Outlook邮件中的嵌入式图像和附件。经过研究后,我发现以下代码适用于大多数情况
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
try
{
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
prop=attachment.PropertyAccessor;
string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
if ((attachmentType.Substring(1).ToLower() == "pdf") ||(conentId==null||conentId.Trim().Length==0))
{
//Always allow PDF
// This is an attachement
}
}
}
catch (Exception ex)
{
}
}
问题是,当从其他邮件系统发送邮件(例如:hotmail)时,附件的内容ID不为null。这将导致附件被忽略。
我讨厌的另一个建议是根据以下StackFlow don't save embedded
检查该物业foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
try
{
// var tst = attachment.Type;
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
prop=attachment.PropertyAccessor;
string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");
var asize = attachment.Size;
if ((attachmentType.Substring(1).ToLower() == "pdf") ||
(asize>0&&(flags!=4 &&(int)attachment.Type != 6))) // 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
// (conentId==null||conentId.Trim().Length==0))
{
//This is a valid attachment
}
}
}
catch (Exception ex)
{
}
}
但这有时会包含签名中的图像
答案 0 :(得分:0)
最可靠的方法是解析HTML正文(MailItem.HTMLBody
属性,以提取所有img
标签并检查其scr
属性。如果其格式为“ cid:xyz”,则“ xyz”将是附件上PR_ATTACH_CONTENT_ID
属性的值。它也可以通过文件名来引用图形文件。
答案 1 :(得分:0)
这是可行的解决方案
var selection = explorer.Selection;
if (selection.Count == 1)
{
object selectedItem = selection[1];
var mailItem = selectedItem as Outlook.MailItem;
if (mailItem == null) return;
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
bool validAttachment = isAnAttachment(attachment);
}
}
private bool isAnAttachment(Outlook.Attachment attachment)
{
bool isValid = false;
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType != null && attachmentType.Trim().Length > 1 && _fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
Outlook.PropertyAccessor prop = attachment.PropertyAccessor;
var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");
var asize = attachment.Size;
// As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachmet
if ((attachmentType.Substring(1).ToLower() == "pdf") || (asize > 0 && flags != 4 && (int)attachment.Type != 6))
{
isValid = true;
}
}
return isValid;
}