我有一个邮件附件,我想从IteAttachment中提取附件,它是电子邮件附件的一部分,我可以提取文件文件附件。
这里要记住的是,ItemAttachment仍然可以在其中再包含一个ItemAttachment,所以我需要递归工作的代码来获取所有的附件,直到它找不到更多的ItemAttachment为止。
发布于 2019-01-08 08:56:55
从官方Microsoft文档中提取的说明和代码示例
下面的代码示例演示如何使用Bind方法获取EmailMessage对象,然后遍历附件集合,并酌情对每个附件调用FileAttachment.Load或ItemAttachment.Load方法。每个文件附件都保存到C:\temp\文件夹中,每个项附件都加载到内存中。有关如何保存项附件的信息,请参阅使用EWS托管API保存附加电子邮件。 此示例假设服务是一个有效的ExchangeService对象,itemId是将从中检索附件的消息的ItemId,并且用户已经通过了对ExchangeService的身份验证。
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);
}
}
}
https://stackoverflow.com/questions/54077174
复制相似问题