【问题标题】:How to reply to an email using the EWS Managed API?如何使用 EWS 托管 API 回复电子邮件?
【发布时间】:2015-11-30 08:08:06
【问题描述】:

我创建了一个使用 EWS 托管 API 2.2 的应用程序。 此应用程序使用拉取通知来获取新电子邮件并将电子邮件副本保存在数据库中。

然后在应用程序中,我想从数据库中获取电子邮件并回复它。 为了回复消息,我需要使用我存储在数据库中的 ItemId 从 EWS 中检索它。

当然我可以创建一个新的 EmailMessage 并发送它,但是新的电子邮件将有一个不同的 ConversationId,这对于应用场景来说是不可接受的。

所以,为了实现这一点,我使用以下代码行 EmailMessage.Bind(service, itemId);

要使此方法起作用,我必须从我的数据库中实例化 ItemId,但 ItemId 构造函数仅将 UniqueId 作为参数,并使用 null ChangeKey 创建它。 如果我使用此 ItemId(ChangeKey 为 null),则会收到以下错误: Microsoft.Exchange.WebServices.Data.ServiceResponseException:在商店中找不到指定的对象。

我认为这是因为空的 ChangeKey。我对么? 有解决办法吗?

【问题讨论】:

    标签: c# exchangewebservices ews-managed-api


    【解决方案1】:

    不要使用 ItemId 来识别消息,而是使用 EntryID。使用 EntryID,您可以绑定到同一封电子邮件,而无需 ChangeKey。

    以下是此类属性的定义:

    ExtendedPropertyDefinition EntryIDProperty = new ExtendedPropertyDefinition(0x0FFF, MapiPropertyType.Binary);
    

    当您搜索消息时,请确保您指示 EWS 在检索到的项目列表中包含此类属性。

    以下是调用 FindItems 时获取 EntryID 的示例:

    ExtendedPropertyDefinition EntryIDProperty = new ExtendedPropertyDefinition(0x0FFF, MapiPropertyType.Binary);
    
    ItemView item_view = new ItemView(10) { PropertySet = new PropertySet(ItemSchema.Id, EntryIDProperty) };
    
    var result = service.FindItems(WellKnownFolderName.Inbox, item_view);
    
    foreach (var item in result.Items)
    {
        byte[] entry_id = (byte[])item.ExtendedProperties.Single(x => x.PropertyDefinition == EntryIDProperty).Value;
    
        string entry_id_hex = ByteArrayToHexString(entry_id); //This is the entry ID that you should store
    }
    

    如果要使用 EmailMessage.Bind,请使用以下方法将 EntryID 转换为 ItemID:

    此方法接受字符串 EntryID。

    mailbox_address是邮箱的SMTP地址(例如test@domain.com)

    'service' 是 ExchangeService 对象。

    private ItemId ConvertEntryIdToItemId(string entryid, string mailbox_address, ExchangeService service)
    {
        AlternateId id = new AlternateId(IdFormat.HexEntryId, entryid, mailbox_address);
    
        AlternateId new_id = (AlternateId)service.ConvertId(id, IdFormat.EwsId);
    
        ItemId item_id = new_id.UniqueId;
    
        return item_id;
    }
    

    现在您可以使用返回的 ItemId 来绑定您的 EmailMessages。

    【讨论】:

      【解决方案2】:

      在商店中找不到指定的对象。

      该错误通常意味着您无权访问您尝试访问的邮箱或您尝试访问的项目不再存在于商店中。例如,在拉取通知应用程序中,这可能意味着您收到通知的项目已被删除或移动到另一个文件夹(在每种情况下,项目都将被分配一个新的 ID)。如果您还列出了 Move 事件,您应该能够看到相应的 move 事件,该事件将具有与 newMailEvent 通知相关的 OldItemId。

      更改密钥仅在您更新项目时才重要,因此如果您在 Bind 上遇到错误意味着您尝试的项目不存在(或已被移动)或您无权访问它绑定只是 UniqueId 完全没问题,另请参阅https://msdn.microsoft.com/en-us/library/office/dn605828(v=exchg.150).aspx

      干杯 格伦

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2015-03-18
        • 2017-05-04
        • 2013-08-25
        相关资源
        最近更新 更多