【发布时间】:2015-01-07 05:51:33
【问题描述】:
让我先说我不使用办公室 COM 对象,并且通常会像瘟疫一样尽量避免它。我也知道这个问题在访问 .pst 文件方面已经被打死了,但是经过一个小时的谷歌搜索后,我还没有找到很多关于 .ost 的信息。
我正在尝试更新现有应用程序中的方法,该应用程序正在访问 .pst 文件并检索要在自动填充实现中使用的联系人姓名和电子邮件列表。我们最近从直接 IMAP 更改为主机电子邮件服务器到 Microsoft Exchange,后者从 .pst 更改为 .ost 文件
这是方法:
var arrName = new List<string>();
var arrEmail = new List<string>();
try
{
var outlookApplication = new ApplicationClass();
NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
for (int i = 1; i < contacts.Items.Count + 1; i++)
{
var contact = (ContactItem) contacts.Items[i];
arrName.Add(contact.FullName);
arrEmail.Add(contact.Email1Address);
}
Global.ConName = arrName.ToArray();
Global.ConEmail = arrEmail.ToArray();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
//Error Logging
}
调用时抛出以下错误:
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to
interface type 'Microsoft.Office.Interop.Outlook.ContactItem'.
This operation failed because the QueryInterface call on the COM
component for the interface with IID '{00063021-0000-0000-C000-000000000046}'
failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at OPUSfin.LoginMain.GetEmailContactsFromOutlook() in PATH:line 81
谁能指出我如何修改它以与 Exchange 实现一起使用的正确方向?
谢谢
【问题讨论】:
标签: c# outlook office-interop