-1
类似于这个问题的其他问题的解决方案已经尝试过,但没有奏效。C#异常 - System.InvalidCastException
关于如何修复它的任何想法?
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
try{
outlookApplication = new Application();
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
mailItems = inboxFolder.Items;
foreach (MailItem email in mailItems){
var emailSubj = email.Subject.ToString();
var emailBody = email.Body;
if (String.IsNullOrWhiteSpace(email.Subject)){
continue;
}
else{
if (email.Subject.StartsWith("Email report test")){
Outlook.Folder folder = outlookApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
Outlook.Folders folders = folder.Folders;
try{
Outlook.Folder newFolder = folders.Add("Email Report test", Type.Missing) as Outlook.Folder;
newFolder.Display();
}
catch{
Console.WriteLine("Email Report test already exists.");
}
finally{
Outlook.MAPIFolder destFolder = folder.Folders["Email Report test"];
email.Move(destFolder);
}
break;
}
}
Marshal.ReleaseComObject(email);
}
}
finally{
ReleaseComObject(mailItems);
ReleaseComObject(inboxFolder);
ReleaseComObject(outlookNamespace);
ReleaseComObject(outlookApplication);
}
}
提前感谢!
点我们错误 –
的foreach(在mailItems的MailItem电子邮件){7号线 – user7943
也行!我忘了提及哪些事情对于这种情况发生的原因相当重要。如果没有找到具有该特定主题的电子邮件,则会发生异常。 – user7943