C# 对Outlook联系人的增、删、查
2024-07-25 08:21:57 215人阅读
原文:C# 对Outlook联系人的增、删、查 [转]
注:定义变量
Outlook.Application myOlApp = newOutlook.ApplicationClass();
Outlook.NameSpace myNameSpace;
Outlook.MAPIFolder myFolder;
一、添加联系人
Outlook.ContactItem addItem =(Outlook.ContactItem)myOlApp.CreateItem(Outlook.OlItemType.olContactItem);
addItem.FirstName = "晶晶";
addItem.LastName = "李";
addItem.Email1Address = "sun@sohu.com";
addItem.Save();
二、删除联系人
Outlook.ContactItem contact =
this.myOlApp.GetNamespace("MAPI").
GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).
Items.
Find(
string.Format("[LastName]=‘{0}‘ AND [FirstName]=‘{1}‘",
lastName, firstName))
as Outlook.ContactItem;
if (contact != null)
{
contact.Delete();
}
三、查询联系人
myNameSpace =myOlApp.GetNamespace("MAPI");//得到MAPI的Session
myFolder =myNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);//获取默认的邮箱信息
int iMailCount = myFolder.Items.Count;
if (iMailCount > 0)
{
for (int k = 1; k <= iMailCount; k++)
{
Outlook.ContactItem item =(Outlook.ContactItem)myFolder.Items[k];
if(!columnMobiles.Contains(item.MobileTelephoneNumber))//添加数据库不存在的联系人
{
string lastName = item.LastName;//姓
string firstName = item.FirstName;//名
string fullName = item.FullName;//全称
//string sex = item.Gender.ToString();//性别
string email = item.Email1Address + "," + item.Email2Address + ","+ item.Email3Address;//Email