[英]Outlook Add-In ComException on GetItemFromID()
我有一个Outlook 2013加载项 ,用于处理传入的电子邮件:
void ThisAddIn_Startup(object sender, EventArgs e)
{
... other code ...
Application.NewMailEx += GetEmail;
...
}
void GetEmail(string ID)
{
try
{
var email = Application.Session.GetItemFromID(ID) as Outlook.MailItem; // COMExeption
var email = Application.Session.GetItemFromID(ID); // No casting - COMException
}
...
}
当收到来自外部(SMTP)电子邮件地址的新电子邮件时,可以很好地处理这些电子邮件。 在测试期间,当我发送日历会议请求时,当该邮件到达时,出现以下异常:
[Exception Type]:
System.Runtime.InteropServices.COMException
[Aggregate Exception]:
The message you specified cannot be found.
[Stack Trace]:
at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetItemFromID(String EntryIDItem, Object EntryIDStore)
at EmailHelper.ThisAddIn.GetEmail(String entryId) in c:\EmailHelper\ThisAddIn.cs:line 44
我尝试了几种不同的解决方法,但是没有运气,总是会抛出异常,例如
GetItemFromID(ID, IDStore)
; GetItemFromID(ID, Type.Missing)
; 我可能会缺少什么?
会议请求不是消息,但是您的代码假定它是MailItem对象。 会议请求由MeetingItem对象表示。
使用“ is”或“ as”运算符,或使用反射来检索Class属性以找出它是哪种对象。
与emily_bma相同。 然后将其EmailItem
为EmailItem
或其他与COMException
无关的东西,这似乎是随机发生的。 有时您会收到此错误,有时则不会。 好像通知是在注册消息之前到来的。 当然,我的Outlook中没有严格的规则,这只是出于开发和测试目的的全新实例。
因此,经过几次尝试和阅读,我想到使用以下代码代替Application.Session.GetItemFromID
实际上会提供更好的结果:
Outlook.NameSpace nameSpace = Application.GetNamespace("MAPI");
var item = nameSpace.GetItemFromID(entry.Trim(), Type.Missing);
至于完整的源代码:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemSend += Application_ItemSend;
Application.NewMailEx += Application_NewMailEx;
}
void Application_NewMailEx(string EntryIDCollection)
{
#if !OLD_METHOD
Outlook.NameSpace nameSpace = Application.GetNamespace("MAPI");
#endif
string[] arr = EntryIDCollection.Split(',');
foreach(string entry in arr)
{
#if OLD_METHOD
var item = Application.Session.GetItemFromID(entry.Trim(), Type.Missing);
#else
var item = nameSpace.GetItemFromID(entry.Trim(), Type.Missing);
#endif
// COMException is thrown there quite often when entry is a meeting.
if (item is Outlook.MailItem)
{
// ... Do something with the email
}
else if (item is Outlook.AppointmentItem)
{
// ... Do something with the appointment
}
else if (item is Outlook.MeetingItem)
{
// ... Do something with the meeting
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.