【发布时间】:2015-11-03 19:41:34
【问题描述】:
我尝试使用 Microsoft Outlook 15.0 对象库 DLL 读取 Microsoft Outlook 联系人,它在本地工作;对于客户端,我们不知道客户端使用的是什么版本的 Outlook。如果每个客户端都有不同版本的 Outlook,如何阅读?
我想使用 C# 读取任何版本的 Microsoft Outlook 版本的联系人。
如果你有任何开源代码,它会很有帮助。
请查看我的代码并帮助我哪里做错了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using MsOutlook = Microsoft.Office.Interop.Outlook;
namespace Test
{
public class OutlookMailManager : IDisposable
{
public OutlookMailManager() { }
/// <summary>
/// Get MailContacts From Google (Gmail) using the provided username and password.
/// </summary>
/// <param name="maxEnries">Total number of entries to return</param>
/// <returns>The addressbook entries</returns>
public string GetOutlookMailContacts(int maxEnries)
{
MsOutlook.ApplicationClass OutlookApplication = new MsOutlook.ApplicationClass();
MsOutlook.NameSpace outlookNameSpace = OutlookApplication.GetNamespace("MAPI");
MsOutlook.MAPIFolder contactsCollection = outlookNameSpace.GetDefaultFolder(MsOutlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Items folderItems = contactsCollection.Items;
string rtnStr = "";
if (folderItems.Count > 0)
{
for (int i = 1; folderItems.Count >= i; i++)
{
object contactObj = folderItems[i];
if (contactObj is MsOutlook.ContactItem)
{
MsOutlook.ContactItem contact = (MsOutlook.ContactItem)contactObj;
rtnStr += contact.FullName + " (" + contact.BusinessTelephoneNumber + ")\n";
}
Marshal.ReleaseComObject(contactObj);
if (i == maxEnries) break;
}
}
Marshal.ReleaseComObject(folderItems);
Marshal.ReleaseComObject(contactsCollection);
Marshal.ReleaseComObject(outlookNameSpace);
return rtnStr;
}
}
}
【问题讨论】:
标签: c# outlook office-interop com-interop