强制转换c#中的对象(将Exchange Web服务项转换为电子邮件)
本文关键字:转换 服务 Web 电子邮件 Exchange 对象 | 更新日期: 2023-09-27 17:57:47
我不知道如何做我需要做的事情。
我有一个名为"MarkAsRead"的方法,它接受ItemID,并且应该将邮件项目标记为已读。
但该项目似乎没有"IsRead"项目,只有一封电子邮件有,所以我需要将我的Exchange WebServices邮件项目转换为电子邮件。
这是代码:
尝试{
System.Diagnostics.Debugger.Break();
//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL);
//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);
//// if the property is not loaded yet, first load it
//mail.Load(PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));
//if (!mail.IsRead) // check that you don't update and create unneeded traffic
//{
// mail.IsRead = true; // mark as read
// mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
//}
// As a best practice, limit the properties returned to only those that are required.
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
// Bind to the existing item by using the ItemId.
// This method call results in a GetItem call to EWS.
Item item = Item.Bind(service, itemId, propSet);
EmailMessage mail = Item.Bind(service, itemId, propSet);
item.Load(new PropertySet(BasePropertySet.IdOnly,EmailMessageSchema.IsRead));
item.IsRead = true;
item.Update(ConflictResolutionMode.AlwaysOverwrite);
return true;
}
我正在尝试做这样的事情:
// if the property is not loaded yet, first load it
mail.Load(PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));
if (!mail.IsRead) // check that you don't update and create unneeded traffic
{
mail.IsRead = true; // mark as read
mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
}
不幸的是,我需要能够从该项目获得一个独特的电子邮件消息。
我该怎么做?
Philip-
-
您可以将EmailMessageSchema.IsRead添加到您的属性集,这样您就可以在Bind调用中获得它,然后您就不必调用Load了。
PropertySet propSet=新的PropertySet(BasePropertySet.IdOnly,ItemSchema.Subject,EmailMessageSchema.IsRead);
-
EmailClass派生自Item类,因此Bind适用于这两个类。因此,您可以执行以下操作:
EmailMessage mail=EmailMessage.Bind(service,itemId,propSet);
把这些放在一起,你就得到了这个:
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, EmailMessageSchema.IsRead);
EmailMessage mail = EmailMessage.Bind(service, itemId, propSet);
if (!mail.IsRead) // check that you don't update and create unneeded traffic
{
mail.IsRead = true; // mark as read
mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
}
Philip,
在测试我的代码时,我只从您的代码中向Item.Bind()
添加了一点点,使其发挥作用:
EmailMessage mail = Item.Bind(service, itemId, propSet) as EmailMessage;
作为EmailMessage会将其强制转换为适合您的类型。之后,我可以看到isRead属性。
我希望这能有所帮助。如果这确实解决了您的问题,请将帖子标记为已回答。
谢谢,
---Bob---