【发布时间】:2015-10-09 03:02:47
【问题描述】:
我写了一个方法来将outlook邮件附件保存到硬盘,并将文件转换成Base64String。当我保存附件时,嵌入的(内联)图像也会被保存,即使它们不是真正的附件。我只想保存真正的附件。然后我将方法修改如下。但是现在我从“.OfType()”行中得到一个错误。
这是我的代码:
private string GetBase64StringForAttachments(Outlook.MailItem mailItem)
{
StringBuilder builder = new StringBuilder();
Outlook.Attachments mailAttachments = mailItem.Attachments;
try
{
if (mailAttachments != null)
{
Regex reg = new Regex(@"<img .+?>", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(mailItem.HTMLBody);
for (int i = 1; i <= mailAttachments.Count; i++)
{
Outlook.Attachment currentAttachment = mailAttachments[i];
bool isMatch = matches
.OfType<Match>()
.Select(m => m.Value)
.Where(s => s.IndexOf("cid:" + currentAttachment.FileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
.Any();
MessageBox.Show(currentAttachment.FileName + ": " + (isMatch ? "Inline Image" : "Attached Image"));
if (currentAttachment != null)
{
string date = DateTime.Now.ToString("yyyymmddhhmmss");
string path = "C:\\test\\" + date + currentAttachment.FileName; //ToDo: Create Folder
currentAttachment.SaveAsFile(path);
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);
builder.Append(encodedData).Append(",");
Marshal.ReleaseComObject(currentAttachment);
}
}
if (builder.Length > 0)
{
string encodedAttachments = builder.ToString().Remove(builder.ToString().Length-1);
return builder.ToString();
}
else
return "";
}
else return "";
}
catch (Exception ex)
{
Debug.DebugMessage(2, "Error in GetBase64StringForAttachments : in AddinModule " + ex.Message);
return "";
}
finally
{
Marshal.ReleaseComObject(mailAttachments);
}
}
这是错误消息:
“System.Text.RegularExpressions.MatchCollection”不包含“OfType”的定义,并且找不到接受“System.Text.RegularExpressions.MatchCollection”类型的第一个参数的扩展方法“OfType”(您是否缺少using 指令还是程序集引用?)
我需要什么:
-
我不是 LINQ 的忠实粉丝。所以,请你给我建议
-
有更好的方法吗?
我已经尝试按照这些问题的建议答案进行操作,但它们对我不起作用
Saving only the REAL attachments of an Outlook MailItem
Don't save embed image that contain into attachements (like signature image)
- 有没有办法使用救赎来区分真正的附件?
【问题讨论】:
-
如果您无法使用标志解决方案,请注意 Microsoft 更改了标志的规则,您必须为您的检查使用不同的值才能使其在 Outlook 365 中工作。有关详细信息,请参阅这个答案stackoverflow.com/a/70823130/1657610.
-
@RegEdit,非常感谢您的关注以及您与我分享的答案。
标签: c# linq outlook outlook-addin outlook-redemption