我在c#中使用VSTO。当我点击按钮时,我将附件保存在文件夹中。我的问题是:当我在签名中有一个带有图像的丰富电子邮件时,我的附件中有一个元素。但我不想保存那个图像。 Outlook(应用程序)在区域附件中隐藏此附件!那我为什么不: - (
我的代码很简单:
MailItem MailItemSelected = this.OutlookItem;
foreach (Attachment a in MailItemSelected.Attachments)
{
a.SaveAsFile(path + a.FileName);
}
但我没有找到测试,不保存签名的图像。
答案 0 :(得分:5)
我们只需要显示"邮件附件"
中的(不是用于渲染的嵌入式)用于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);
}
}
}
}
答案 1 :(得分: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 :(得分:2)
看到这个问题有+ 2k点击但仍未得到解答,这是我尝试使用静态实用工具方法返回非内联附件列表:
/// <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;
}
答案 3 :(得分: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主体中检查标签中是否提到了每个附件。
编辑:如果您在正文中键入其名称,则上一个方法可能会跳过附件。这不像是跳过误报
if (!mailItem.HTMLBody.Contains("cid:" + attachment.FileName))