如何区分 Outlook 2010 中的内联图像和附件 [C#]
Posted
技术标签:
【中文标题】如何区分 Outlook 2010 中的内联图像和附件 [C#]【英文标题】:How to differentiate between Inline image and attachment in outlook 2010 [C#] 【发布时间】:2012-05-13 19:47:37 【问题描述】:您好,我必须使用 C# 从 Outlook 2010 的本地目录中分别读取附件和内联图像。我为此使用了属性和内容 ID 概念。我正在使用以下代码来执行此操作,但它现在可以工作了。
if (mailItem.Attachments.Count > 0)
/*for (int i = 1; i <= mailItem.Attachments.Count; i++)
string filePath = Path.Combine(destinationDirectory, mailItem.Attachments[i].FileName);
mailItem.Attachments[i].SaveAsFile(filePath);
AttachmentDetails.Add(filePath);
*/
foreach (Outlook.Attachment atmt in mailItem.Attachments)
MessageBox.Show("inside for each loop" );
prop = atmt.PropertyAccessor;
string contentID = (string)prop.GetProperty(SchemaPR_ATTACH_CONTENT_ID);
MessageBox.Show("content if is " +contentID);
if (contentID != "")
MessageBox.Show("inside if loop");
string filePath = Path.Combine(destinationDirectory, atmt.FileName);
MessageBox.Show(filePath);
atmt.SaveAsFile(filePath);
AttachmentDetails.Add(filePath);
else
MessageBox.Show("inside else loop");
string filePath = Path.Combine(destinationDirectoryT, atmt.FileName);
atmt.SaveAsFile(filePath);
AttachmentDetails.Add(filePath);
请帮助正在进行的工作......
【问题讨论】:
字符串 SchemaPR_ATTACH_CONTENT_ID = @"schemas.microsoft.com/mapi/proptag/0x3712001E"; 【参考方案1】:这对我有用 - 检查附件名称是否存在于电子邮件的 htmlBody 中
VB.NET
<i>
For Each oAttachment In m_olMailItem.Attachments
If m_olMailItem.HTMLBody.ToLower.Contains("cid:" & oAttachment.FileName) = True Then
Msgbox("Embedded")
Else
Msgbox("Not Embedded ! ")
End if
http://www.outlookforums.com/threads/44155-detecting-if-attachement-is-embedded-or-not/
【讨论】:
【参考方案2】:我来这里是为了寻找解决方案,但不喜欢在整个 HTMLBody 中搜索“cid:”的想法。首先,对每个文件名都这样做很慢,其次,如果正文中出现“cid:”,我会得到误报。此外,在 HTMLBody 上执行 ToLower() 也不是一个好主意。
相反,我最终在 HTMLBody 上使用了一个正则表达式来查找 标记的任何实例。因此,无法在正文中错误地匹配“cid:”(尽管这不太可能)。
Regex reg = new Regex(@"<img .+?>", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(mailItem.HTMLBody);
foreach (string fileName in attachments.Select(a => a.FileName)
bool isMatch = matches
.OfType<Match>()
.Select(m => m.Value)
.Where(s => s.IndexOf("cid:" + fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
.Any();
Console.WriteLine(fileName + ": " + (isMatch ? "Inline" : "Attached"));
我很确定我可以编写一个正则表达式来仅返回文件名,并且它可能会更有效。但我宁愿为那些必须维护代码的非正则表达式专家的可读性付出额外的费用。
【讨论】:
如果邮件中包含带有某种图片的签名,这将不起作用。 Outlook 不会包含在我知道这是一个老问题,但答案可能对某人有所帮助。
using Attachment = MsgReader.Outlook.Storage.Attachment;
foreach (Attachment attachment in mailItem.Attachments.Where(a => ((Attachment)a).Hidden == false))
// do whatever you want with the 'real' attachments.
【讨论】:
以上是关于如何区分 Outlook 2010 中的内联图像和附件 [C#]的主要内容,如果未能解决你的问题,请参考以下文章
试图在 html 电子邮件中隐藏图像 - 在 Outlook 2010 中不起作用
如何解析带有附件和内联图像的 EML 文件并转换为 HTML