C# Exchange Web 服务 - 发送并立即检索已发送的消息
C# Exchange web service - send and immediately retrieve sent message
我正在使用的系统要求将刚刚发送的电子邮件的 MimeContent
存储在本地数据库中。我对 Exchange 工作原理的理解是它将在服务器上创建 MimeContent
并且我无法访问它,除非我查询服务以获取刚刚发送的消息。
所以,我采取的步骤是:
-- 发送邮件并获取 Id
message.SendAndSaveCopy();
return message.Id.UniqueId;
-- 使用新的id获取刚刚发送的EmailMessage
ExchangeService exchangeService = ExchangeService;
var properties = new List<PropertyDefinitionBase>
{
ItemSchema.MimeContent
};
EmailMessage message = EmailMessage.Bind(exchangeService, new ItemId(messageId), new PropertySet(BasePropertySet.IdOnly, properties));
当这段代码不间断地运行时,它就可以工作。返回的 Id
仍然有效(邮件可能在发件箱文件夹中),我得到了结果。但是,如果我放慢速度一秒钟,Id 将不再有效(我猜它现在已移至已发送文件夹)。
我不能就这样离开,因为不能保证我会及时返回服务器以检索带有 Id
的消息。
如果有一个解决方案让我不必再次查询消息服务,那就太棒了。但是,如果没有,有没有办法可以使用 Id
和 ChangeKey
来获取刚刚发送的电子邮件?
终于回答了我自己的问题。我通过在我控制的每封电子邮件上添加自定义 属性 来做到这一点。然后我使用相同的值来搜索电子邮件。我在查询之间的文件夹之间移动电子邮件时遇到一些问题,但现在已排序。
在某处定义它,我在我的 EmailProvider 中有它 class:
private readonly PropertyDefinitionBase _TempEmailId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "TempEmailId", MapiPropertyType.String);
然后在发送电子邮件之前添加它:
string messageId = null;
if (getTempId)
{
messageId = Guid.NewGuid().ToString();
message.SetExtendedProperty((ExtendedPropertyDefinition) _TempEmailId, messageId);
}
message.SendAndSaveCopy();
return messageId;
最后,使用 messageId
获取 MimeContent
(或您想要的任何其他属性):
/// <summary>
/// Get the mime content for an email just sent
/// </summary>
/// <param name="messageId"></param>
/// <returns></returns>
public byte[] GetJustSentMimeContent(string messageId)
{
ExchangeService exchangeService = ExchangeService;
// Use the following search filter to get mail with TempEmailId set to what we just got
var searchCriteria = new SearchFilter.IsEqualTo(_TempEmailId, messageId);
var itemView = new ItemView(1) {PropertySet = new PropertySet(BasePropertySet.IdOnly)};
byte[] GetMimeContent(WellKnownFolderName folder)
{
var items = exchangeService.FindItems(folder, searchCriteria, itemView);
if (items.TotalCount > 0)
{
// if it's still in there, try and load the properties for it
exchangeService.LoadPropertiesForItems(items,
new PropertySet(BasePropertySet.IdOnly,
ItemSchema.MimeContent));
var emailMessage = (EmailMessage) items.First();
// if the content has been loaded, return it
if (emailMessage.MimeContent != null)
return emailMessage.MimeContent.Content;
}
return null;
}
// check the drafts folder to see if it's still in there
var mimeContent = GetMimeContent(WellKnownFolderName.Drafts);
if (mimeContent != null)
return mimeContent;
// if at this point, either:
// - email was moved to SentItems before the first search was done
// - or email was found in Drafts but then moved to SentItems but before the MimeContent could be loaded. Need to restart the process and find the email in SentItems instead
// should be in here (sentItems) now, try and load the properties for it
return GetMimeContent(WellKnownFolderName.SentItems);
}
我正在使用的系统要求将刚刚发送的电子邮件的 MimeContent
存储在本地数据库中。我对 Exchange 工作原理的理解是它将在服务器上创建 MimeContent
并且我无法访问它,除非我查询服务以获取刚刚发送的消息。
所以,我采取的步骤是:
-- 发送邮件并获取 Id
message.SendAndSaveCopy();
return message.Id.UniqueId;
-- 使用新的id获取刚刚发送的EmailMessage
ExchangeService exchangeService = ExchangeService;
var properties = new List<PropertyDefinitionBase>
{
ItemSchema.MimeContent
};
EmailMessage message = EmailMessage.Bind(exchangeService, new ItemId(messageId), new PropertySet(BasePropertySet.IdOnly, properties));
当这段代码不间断地运行时,它就可以工作。返回的 Id
仍然有效(邮件可能在发件箱文件夹中),我得到了结果。但是,如果我放慢速度一秒钟,Id 将不再有效(我猜它现在已移至已发送文件夹)。
我不能就这样离开,因为不能保证我会及时返回服务器以检索带有 Id
的消息。
如果有一个解决方案让我不必再次查询消息服务,那就太棒了。但是,如果没有,有没有办法可以使用 Id
和 ChangeKey
来获取刚刚发送的电子邮件?
终于回答了我自己的问题。我通过在我控制的每封电子邮件上添加自定义 属性 来做到这一点。然后我使用相同的值来搜索电子邮件。我在查询之间的文件夹之间移动电子邮件时遇到一些问题,但现在已排序。
在某处定义它,我在我的 EmailProvider 中有它 class:
private readonly PropertyDefinitionBase _TempEmailId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "TempEmailId", MapiPropertyType.String);
然后在发送电子邮件之前添加它:
string messageId = null; if (getTempId) { messageId = Guid.NewGuid().ToString(); message.SetExtendedProperty((ExtendedPropertyDefinition) _TempEmailId, messageId); } message.SendAndSaveCopy(); return messageId;
最后,使用
messageId
获取MimeContent
(或您想要的任何其他属性):/// <summary> /// Get the mime content for an email just sent /// </summary> /// <param name="messageId"></param> /// <returns></returns> public byte[] GetJustSentMimeContent(string messageId) { ExchangeService exchangeService = ExchangeService; // Use the following search filter to get mail with TempEmailId set to what we just got var searchCriteria = new SearchFilter.IsEqualTo(_TempEmailId, messageId); var itemView = new ItemView(1) {PropertySet = new PropertySet(BasePropertySet.IdOnly)}; byte[] GetMimeContent(WellKnownFolderName folder) { var items = exchangeService.FindItems(folder, searchCriteria, itemView); if (items.TotalCount > 0) { // if it's still in there, try and load the properties for it exchangeService.LoadPropertiesForItems(items, new PropertySet(BasePropertySet.IdOnly, ItemSchema.MimeContent)); var emailMessage = (EmailMessage) items.First(); // if the content has been loaded, return it if (emailMessage.MimeContent != null) return emailMessage.MimeContent.Content; } return null; } // check the drafts folder to see if it's still in there var mimeContent = GetMimeContent(WellKnownFolderName.Drafts); if (mimeContent != null) return mimeContent; // if at this point, either: // - email was moved to SentItems before the first search was done // - or email was found in Drafts but then moved to SentItems but before the MimeContent could be loaded. Need to restart the process and find the email in SentItems instead // should be in here (sentItems) now, try and load the properties for it return GetMimeContent(WellKnownFolderName.SentItems); }