[英]C# outlook : Unable to cast COM object of type 'System._ComObject' to Interface type 'Microsoft.Office.Interop.Outlook.MailItem.'
I'm new to c#.我是 c# 的新手。
I have been trying on my own to fix this issue since 1 week until i've got no other alternative than to ask for help here.自 1 周以来,我一直在尝试自己解决此问题,直到我别无选择,只能在这里寻求帮助。
My goal is to create a console app which would look for Unread Mails on the secondary mailbox[mailboxB@gmail.com]/second outlook account and reply those mails based on the Subject.我的目标是创建一个控制台应用程序,它将在辅助邮箱 [mailboxB@gmail.com]/second outlook 帐户上查找未读邮件并根据主题回复这些邮件。
The code works fine on my Machine .该代码在我的 Machine 上运行良好。
Unfortunately when the same exe is deployed on other machine , the following error gets generated:不幸的是,当在其他机器上部署相同的 exe 时,会生成以下错误:
unable to cast com object of 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)) unable to cast com object of 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出现以下错误:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE))
Please note i've tried the following to resolve the issue but the issue still prevails:请注意,我已尝试以下方法来解决问题,但问题仍然存在:
Restarted Outlook as administrator.以管理员身份重新启动 Outlook。
Change the compatibility of the app under properties-> compatibility to run as administrator在属性->兼容性下更改应用程序的兼容性以管理员身份运行
Checked the outlook version which is 16.0 and the same ref is being used for in the code.检查了 outlook 版本,它是 16.0,并且在代码中使用了相同的 ref。
Please find my code below:请在下面找到我的代码:
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("MAPI");
MAPIFolder theInbox = oNS.Folders["mailboxB@gmail.com"].Folders["Inbox"];
Outlook.Items unreadItems = theInbox.Items.Restrict("[Unread]=true");
string t = "Test Mail";
try
{
foreach (Outlook.MailItem it in unreadItems)
{
if (it.Subject == t)
{
Outlook.MailItem replyMail = it.Reply();
it.HTMLBody = DateTime.Now.ToString("HH:mm:ss");
replyMail.Send();
it.Unread=false;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Any sort of help would be highly appreciated.任何形式的帮助将不胜感激。
You can have items other than MailItem
in your Inbox folder, eg ReportItem
or MeetingItem
.您的收件箱文件夹中可以包含MailItem
以外的项目,例如ReportItem
或MeetingItem
。
Change your code to将您的代码更改为
foreach (object obj in unreadItems)
{
if (obj is Outlook.MailItem it)
{
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.