使用任何Microsoft Outlook版本在C#中阅读Outlook联系人
我曾尝试使用Microsoft Outlook 15.0对象库DLL读取Microsoft Outlook联系人,它在本地工作;说到客户端,我们不知道客户端使用的是什么版本的Outlook。如何读取每个客户端是否具有不同版本的Outlook 我想使用C#阅读任何版本的Microsoft Outlook版本的联系人 如果你有任何开源代码,它会帮助你很多 请看我的代码,并帮助我哪里做错了使用任何Microsoft Outlook版本在C#中阅读Outlook联系人,c#,outlook,office-interop,com-interop,C#,Outlook,Office Interop,Com Interop,我曾尝试使用Microsoft Outlook 15.0对象库DLL读取Microsoft Outlook联系人,它在本地工作;说到客户端,我们不知道客户端使用的是什么版本的Outlook。如何读取每个客户端是否具有不同版本的Outlook 我想使用C#阅读任何版本的Microsoft Outlook版本的联系人 如果你有任何开源代码,它会帮助你很多 请看我的代码,并帮助我哪里做错了 using System; using System.Collections.Generic; using Sy
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Text.RegularExpressions;
使用系统线程;
使用System.Runtime.InteropServices;
使用MsOutlook=Microsoft.Office.Interop.Outlook;
名称空间测试
{
公共类Outlook邮件管理器:IDisposable
{
public OutlookMailManager(){}
///
///使用提供的用户名和密码从Google(Gmail)获取MailContacts。
///
///要返回的项目总数
///通讯录条目
公共字符串GetOutlookMailContacts(int-maxEnries)
{
MsOutlook.ApplicationClass OutlookApplication=新建MsOutlook.ApplicationClass();
MsOutlook.NameSpace-outlookNameSpace=OutlookApplication.GetNamespace(“MAPI”);
MsOutlook.MAPIFolder contacts集合=outlookNameSpace.GetDefaultFolder(MsOutlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Items folderItems=联系人集合.Items;
字符串rtnStr=“”;
如果(folderItems.Count>0)
{
对于(int i=1;folderItems.Count>=i;i++)
{
对象contactObj=folderItems[i];
if(contactObj是MsOutlook.ContactItem)
{
MsOutlook.ContactItem contact=(MsOutlook.ContactItem)contactObj;
rtnStr+=contact.FullName+“(“+contact.BusinessTelephoneNumber+”)\n”;
}
Marshal.ReleaseComObject(contactObj);
如果(i==maxEnries)中断;
}
}
元帅发布对象(folderItems);
Marshal.ReleaseComObject(contactsCollection);
Marshal.ReleaseComObject(outlookNameSpace);
返回rtnStr;
}
}
}
您只需使用与需要支持的最低Outlook版本相对应的PIA即可。因此,您将确保只使用所有Outlook版本中存在的属性和方法。有关示例项目,请参见。当前,它与my Outlook 2003版本配合使用效果良好
签出此代码,但我尚未使用不同的outlook版本进行测试。但是
在引用中添加Microsoft.Office.Interop.Outlook dll
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj;
MAPIFolder Folder_Contacts;
private void Form1_Load(object sender, EventArgs e)
{
outlookObj = new Microsoft.Office.Interop.Outlook.Application();
Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;
for (int i = 0; i < OutlookItems.Count; i++)
{
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
MessageBox.Show("FirstName:"contact.FirstName +" "+"LastName:"+contact.LastName +" "+"Emailid:"+contact.Email1Address);
}
}
Microsoft.Office.Interop.Outlook.Items-OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj;
MAPIFolder\u联系人;
私有void Form1\u加载(对象发送方、事件参数e)
{
outlookObj=新的Microsoft.Office.Interop.Outlook.Application();
Folder_Contacts=(MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems=文件夹_Contacts.Items;
对于(int i=0;i
Hi Eugene谢谢你的帖子,我目前只使用PIA,它只对一些客户有效。为什么它不能在其他客户端上工作,可能是什么问题。@Sam在这些客户端上生成的错误消息是什么?抛出的完整异常是什么?如果您没有记录它,您应该这样做。如果没有它,人们只能猜测他们的版本可能比您使用的PIA版本旧,或者可能存在比特问题(x86 vs x64)。我想知道,您是否尝试过这一版本,如果它的投票结果令人振奋,那么它将对其他人有所帮助。