【问题标题】:Saving msg file attachment using EWS API使用 EWS API 保存 msg 文件附件
【发布时间】:2016-05-31 21:16:52
【问题描述】:
希望有人可以提供帮助。我正在实现一项功能,您可以在其中选择电子邮件附件并将其保存在数据库中。该功能适用于 PDF 等,但是当涉及到 MSG 文件时,它会创建一个 ItemAttachment 而不是 Fileattachment,并且不让我能够获取内容或内容类型。
我发现this 发布了关于将消息保存为 .eml 的帖子,但理想情况下,电子邮件附有 .msg,这应该保存在系统中。我做了一些广泛的搜索,但已经走到了死胡同。
我正在使用 asp.net,因此将不胜感激 VB.net 中的答案。
谢谢
【问题讨论】:
标签:
vb.net
exchange-server
exchangewebservices
ews-managed-api
【解决方案1】:
编辑:对不起,我没有看到你在使用 VB,现在我不会删除我的帖子,因为它仍然可以让你知道如何处理这个问题,我知道当我遇到这个问题时,我的想法of process 只是不正确,实际语法不是挑战。
我对这个网站还很陌生,如果我的答案格式不正确,请见谅。
假设您已经建立了交换连接,创建了新的 ItemView,并从您的收件箱中检索了所有电子邮件,我们将从创建电子邮件的辅助列表开始,但我们只会列出包含项目附件的电子邮件.
List<EmailMessage> emailsWithItemAttachment =
emails.Where(e => e.HasAttachments && e.Attachments[0] is ItemAttachment).ToList();
现在,我们可以只循环带有项目附件的电子邮件
foreach (EmailMessage emailMessage in emailsWithItemAttachment)
{
//Loads all emails with Item attachments as an item attachment
foreach (Attachment attachment in emailMessage.Attachments)
{
attachment.Load();
ItemAttachment itemAttachment = attachment as ItemAttachment;
if (itemAttachment == null) continue;
ItemAttachment itemattachment = attachment as ItemAttachment;
itemattachment.Load(new PropertySet(ItemSchema.Attachments));
//Loads the scanned Attachment as an Item Attachment
foreach (Attachment scannedAttachment in itemattachment.Item.Attachments)
{
scannedAttachment.Load();
//Loads all Item Attachments as File Attachments
FileAttachment fileAttachment = scannedAttachment as FileAttachment;
if (fileAttachment != null)
{
//All Done! Your attachment will be "fileAttachment", from here you can do whatever you want
}
}
}
}
我真的希望这对您有所帮助,如果我的回答有任何问题,请随时编辑和/或与我联系!
【解决方案2】:
查询 ItemAttachment 时,添加 MimeContentPropertyDefinition(抱歉,不记得确切的名称和类)。然后,您的 ItemAttachment 将设置该 MimeContent 属性 - 这是一个文本 (MIME),您可以将其保存到 UTF-8 编码并带有 .EML 扩展名的文件中。 MIME 是一种标准,因此任何邮件客户端应用程序都可以打开 .EML 文件。