我有一个邮件附件,我想从IteAttachment中提取附件,该附件是电子邮件附件的一部分。我可以提取文件附件。
这里要记住的一点是ItemAttachment内还可以再有一个ItemAttachment,因此我希望代码可以递归地工作以获取所有附件,直到找不到更多的ItemAttachment。
答案 0 :(得分:0)
解释和代码示例摘自Microsoft官方文档
Get attachments from an email by using the EWS Managed API
以下代码示例显示如何通过以下方式获取EmailMessage对象: 使用Bind方法,然后遍历附件集合 并在每个上调用FileAttachment.Load或ItemAttachment.Load方法 适当的附件。每个文件附件都保存到 C:\ temp \文件夹,并将每个项目附件加载到内存中。对于 有关如何保存项目附件的信息,请参阅保存附件 使用EWS托管API发送电子邮件。
此示例假定服务是有效的ExchangeService对象, 该itemId是邮件的ItemId,附件将从中发送 被检索,并且用户已通过Exchange身份验证 服务器。
public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId)
{
// Bind to an existing message item and retrieve the attachments collection.
// This method results in an GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the attachment into a file.
// This call results in a GetAttachment call to EWS.
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
Console.WriteLine("File attachment name: " + fileAttachment.Name);
}
else // Attachment is an item attachment.
{
ItemAttachment itemAttachment = attachment as ItemAttachment;
// Load attachment into memory and write out the subject.
// This does not save the file like it does with a file attachment.
// This call results in a GetAttachment call to EWS.
itemAttachment.Load();
Console.WriteLine("Item attachment name: " + itemAttachment.Name);
}
}
}