首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用EWS在ItemAttachment中获取附件

如何使用EWS在ItemAttachment中获取附件
EN

Stack Overflow用户
提问于 2019-01-07 15:25:09
回答 1查看 1.4K关注 0票数 0

我有一个邮件附件,我想从IteAttachment中提取附件,它是电子邮件附件的一部分,我可以提取文件文件附件。

这里要记住的是,ItemAttachment仍然可以在其中再包含一个ItemAttachment,所以我需要递归工作的代码来获取所有的附件,直到它找不到更多的ItemAttachment为止。

EN

回答 1

Stack Overflow用户

发布于 2019-01-08 08:56:55

从官方Microsoft文档中提取的说明和代码示例

使用EWS托管API从电子邮件中获取附件

下面的代码示例演示如何使用Bind方法获取EmailMessage对象,然后遍历附件集合,并酌情对每个附件调用FileAttachment.Load或ItemAttachment.Load方法。每个文件附件都保存到C:\temp\文件夹中,每个项附件都加载到内存中。有关如何保存项附件的信息,请参阅使用EWS托管API保存附加电子邮件。 此示例假设服务是一个有效的ExchangeService对象,itemId是将从中检索附件的消息的ItemId,并且用户已经通过了对ExchangeService的身份验证。

代码语言:javascript
运行
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54077174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档