Outlook 获取联系人名片对象为空

在Outlook中有Outlook联系人和全球地址簿,在Outlook开发中ContactItem对象指的是Outlook联系人。

所以当需要通过MailItem对象的Recipients得到收件人联系人名片时,可以使用如下方法:

<pre class="csharp" name="code">Recipients recipients = mailItem.Recipients;
foreach (Recipient rec in recipients)
{
    resolved = rec.Resolve();
    if (resolved)
    {
       ContactItem contactItem = rec.AddressEntry.GetContact();
       String companyName = contactItem.CompanyName;       
       // ...
    }
}
 

当我们需要在ItemSend中获取收件人ContactItem时,需要从联系人中指定,不要从全球地址通讯簿指定,否则rec.AddressEntry.GetContact();为空。


MSDN中GetContact():http://msdn.microsoft.com/en-us/library/bb175073(v=office.12).aspx



Outlook开发交流 Q群:365895439365895439



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取Outlook联系人的详细步骤及代码如下: 1. 引入Outlook头文件 ```c++ #include <afxwin.h> #include <afxdisp.h> #include <atlbase.h> #include <atlcom.h> #include <atlctl.h> #include <oleauto.h> #include <msoutl.h> ``` 2. 定义COleException异常类 ```c++ class COleException : public CException { public: COleException(HRESULT hr); virtual ~COleException() throw(); // Attributes public: HRESULT m_hr; // Implementation public: virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL); }; ``` 3. 实现COleException异常类 ```c++ COleException::COleException(HRESULT hr) : m_hr(hr) { } COleException::~COleException() throw() { } BOOL COleException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext) { CString str; str.Format(_T("0x%08x"), m_hr); lstrcpyn(lpszError, str, nMaxError); return TRUE; } ``` 4. 定义COutlookContacts类 ```c++ class COutlookContacts { public: COutlookContacts(); virtual ~COutlookContacts(); // Operations public: BOOL CreateDispatch(LPCTSTR lpszProgID); void CloseDispatch(); COleException m_exception; _Application m_app; _NameSpace m_session; _Items m_items; _MailItem m_mailItem; // Implementation protected: void ThrowException(HRESULT hr); }; ``` 5. 实现COutlookContacts类 ```c++ COutlookContacts::COutlookContacts() { } COutlookContacts::~COutlookContacts() { CloseDispatch(); } BOOL COutlookContacts::CreateDispatch(LPCTSTR lpszProgID) { COleException e; if (!m_app.CreateDispatch(lpszProgID, &e)) { ThrowException(e.m_hr); return FALSE; } m_session.AttachDispatch(m_app.GetNamespace(_T("MAPI"))); try { m_session.Logon(_T(""), _T(""), TRUE, TRUE); m_items.AttachDispatch(m_session.GetDefaultFolder(olFolderContacts)->GetItems(), TRUE); } catch (COleException* pEx) { ThrowException(pEx->m_hr); pEx->Delete(); return FALSE; } return TRUE; } void COutlookContacts::CloseDispatch() { m_items.ReleaseDispatch(); m_session.ReleaseDispatch(); m_app.ReleaseDispatch(); } void COutlookContacts::ThrowException(HRESULT hr) { m_exception.m_hr = hr; THROW(m_exception); } ``` 6. 使用COutlookContacts类获取联系人信息 ```c++ COutlookContacts outlook; if (outlook.CreateDispatch(_T("Outlook.Application"))) { int count = outlook.m_items.GetCount(); for (int i = 1; i <= count; i++) { _ContactItem contactItem = outlook.m_items.GetItem(COleVariant((short)i)); CString name = contactItem.GetFullName(); CString email = contactItem.GetEmail1Address(); // Do something with the name and email } outlook.CloseDispatch(); } else { AfxMessageBox(_T("Failed to create Outlook session.")); } ``` 注意:需要在项目属性中的“C/C++” -> “预处理器” -> “预处理器定义”中添加“_ATL_APARTMENT_THREADED”宏定义。另外,需要在程序运行时保证Outlook已经启动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值