首页 > 代码库 > C# 对Outlook联系人的增、删、查 

C# 对Outlook联系人的增、删、查 

原文: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
                       string job = item.JobTitle;//职务
                       string department = item.Department;//部门
                       string company = item.CompanyName;//单位
                       string mobile = item.MobileTelephoneNumber;//手机
                       string birthday = "";
                       if (!item.Birthday.ToString().Equals("4501-1-1 0:00:00"))
                           birthday =item.Birthday.ToString();//生日                   

                       //item.PagerNumber//寻呼机
                       string corFax = item.BusinessFaxNumber;//商务传真
                       string homePage = item.BusinessHomePage;//网页地址
                       string corPhone = item.BusinessTelephoneNumber;//商务电话
                       string corNation = item.BusinessAddressCountry;//国家
                       string corProvince = item.BusinessAddressState;//省
                       string corArea = item.BusinessAddressCity;//城市
                       string corAddress = item.BusinessAddressStreet;//地址
                       string corPostCode = item.BusinessAddressPostalCode;//邮政编码


                       string nation = item.HomeAddressCountry;//住宅国家
                       string province = item.HomeAddressState;//住宅省份
                       string area = item.HomeAddressCity;//住宅城市
                       string postCode = item.HomeAddressPostalCode;//住宅邮编
                       string address = item.HomeAddressStreet;//住宅地址
                       string handSet = item.HomeTelephoneNumber;//住宅电话
                       string fax = item.HomeFaxNumber;//住宅传真


                       //item.BusinessAddress//组合地址 (河北省衡水市安平县)
                       //item.FormDescription.Icon//不明确图片
                       //item.HasPicture//是否有图片
                       //item.HomeAddress//住宅地址
                       //item.PrimaryTelephoneNumber//主要电话
                       //item.ManagerName//经理名称

                       

                       //执行添加
                       bool result = AssociateManager.AddAssociate(associate);

                       if (k == iMailCount)
                       {
                           MessageBox.Show("导入数据成功!", "系统提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
                           this.Close();//关闭窗口
                       }
                   }
               }
           }
           else
               MessageBox.Show("对不起,没有导入的数据!", "系统提示", MessageBoxButtons.OK,MessageBoxIcon.Information);


转自:http://blog.csdn.net/as7616263/archive/2010/05/25/5623222.aspx

C# 对Outlook联系人的增、删、查