如何使用EWS托管API回复电子邮件? [英] How to reply to an email using the EWS Managed API?

查看:110
本文介绍了如何使用EWS托管API回复电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created an application that uses the EWS Managed API 2.2. This application uses pull notifications to get new emails and saves a copy of the email in a database.

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

Then in the application I want to get the email from database and reply to it. In order to reply to the message, I need to retrieve it from EWS using the ItemId I have stored in my database.

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

Of course I can create a new EmailMessage and send it but then the new email will have a different ConversationId which is not acceptable for the application scenario.

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

So, in order to achieve this I use the the following line of code EmailMessage.Bind(service, itemId);

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

For this method to work I have to instantiate the ItemId from my database but the ItemId constructor takes as parameter only the UniqueId, and creates it with null ChangeKey. If I use this ItemId (with null ChangeKey) I get the following error: Microsoft.Exchange.WebServices.Data.ServiceResponseException: The specified object was not found in the store.

我认为这是因为无效的ChangeKey.我对么? 有没有解决方法?

I think that this is because of the null ChangeKey. Am I correct? Is there a workaround about this?

推荐答案

使用EntryID代替通过ItemId标识消息.使用EntryID,您可以绑定到同一封电子邮件,而无需ChangeKey.

Instead of identifying a message by ItemId, use EntryID. Using EntryID, you can bind to the same email without needing ChangeKey.

以下是此类属性的定义:

Here is the definition of such property:

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

搜索消息时,请确保指示EWS将此类属性包括在检索到的项目列表中.

When you do your search for messages, make sure you instruct EWS to include such property in the list of retrieved items.

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

Here is an example to obtain EntryIDs when you invoke FindItems:

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:

Use the following method to convert a EntryID to ItemID if you want to use EmailMessage.Bind:

此方法接受字符串EntryID.

This method accepts string EntryID.

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

mailbox_address is the SMTP address of the mailbox (e.g. test@domain.com)

服务"是ExchangeService对象.

'service' is the ExchangeService object.

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.

Now you can use the returned ItemId to bind your EmailMessages.

这篇关于如何使用EWS托管API回复电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆