【问题标题】:Get only text of Item from ItemAttachment (EWS Managed API)从 ItemAttachment 仅获取项目的文本(EWS 托管 API)
【发布时间】:2014-05-04 11:34:21
【问题描述】:

有没有办法从 ItemAttachment 的 Item 中删除 HTML 标签? 我只能从 Item 中获取文本。但不是来自 ItemAttachment 的 Item。 这是我的代码:

foreach (ItemAttachment itemAttach in item.Attachments.OfType<ItemAttachment>())
{
    Console.WriteLine(itemAttach.Name);

    itemAttach.Load();

    PropertySet propSet = new PropertySet();
    propSet.RequestedBodyType = BodyType.Text;
    propSet.BasePropertySet = BasePropertySet.FirstClassProperties;

    itemAttach.Item.Load(propSet);

    Console.WriteLine(itemAttach.Item.Body.Text);
}

它会得到这个异常

This operation isn't supported on attachments

我尝试使用项目 ID 绑定到交换服务。

这也给了我一些例外! 请给我一些建议,我该怎么做。

【问题讨论】:

    标签: c# properties attachment ews-managed-api


    【解决方案1】:

    金,

    您遇到的异常与您正在创建的属性集有关。我没有看到您获取物品的代码,所以我无法确定确切的原因。我能够让以下代码在我的机器上工作。您应该能够根据需要对其进行修改。

    // Return the first ten items.
    ItemView view = new ItemView(10);
    
    // Set the query string to only find emails with attachments.
    string querystring = "HasAttachments:true Kind:email";
    
    // Find the items in the Inbox.
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);
    
    // Loop through the results.
    foreach (EmailMessage email in results)
    {
        // Load the email message with the attachments
        email.Load(new PropertySet(EmailMessageSchema.Attachments));
    
        // Loop through the attachments.
        foreach (Attachment attachment in email.Attachments)
        {
            // Only process item attachments.            
            if (attachment is ItemAttachment)
            {
                ItemAttachment itemAttachment = attachment as ItemAttachment;
    
                // Load the attachment.
                itemAttachment.Load(new PropertySet(EmailMessageSchema.TextBody));
    
                // Output the body.
                Console.WriteLine(itemAttachment.Item.TextBody);
            }
    }
    

    对于每封带有项目附件的电子邮件,我都能够看到已删除 HTML 标记的项目正文。

    我希望这会有所帮助。如果这解决了您的问题,请将此帖子标记为已回答。

    谢谢,

    --- 鲍勃 ---

    【讨论】:

    • 感谢您的代码。但我认为我的程序不能使用 TextBody 属性,因为我的 Exchange Server 是 2010。如果我没记错的话,TextBody 适用于 Exchange Server 2013。我改变了我的代码PropertySet propSet = new PropertySet(); propSet.RequestedBodyType = BodyType.Text; itemAttach.Load(propSet); 它也不起作用。但这一次,没有发现异常!我不知道为什么它不起作用。
    • 我在示例中更改了几行代码,与您所做的类似,并且能够输出消息的正文。问题仍然存在,即没有删除 HTML 标记,因此您必须解析文本。这是另一篇展示如何删除 HTML 标签的帖子:stackoverflow.com/questions/4878452/remove-html-tags-in-string
    • 谢谢鲍勃!我会考虑解析。目前,根据我的观察,附件是ItemAttachment的可能性很小。
    • 要记住的另一件事是,当您确定附件是 ItemAttachment 时,您还需要确保附件是 EmailMessage 项目类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2012-12-02
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多