Thursday, June 4, 2009

Get Outlook Contact in C#.net

public DataTable GetOutlookContact(string sender)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, true, true);
Outlook.MAPIFolder oContacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = (Outlook.Items)oContacts.Items;
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("FullName"));
dt.Columns.Add(new DataColumn("PagerNumber"));
dt.Columns.Add(new DataColumn("MobileTelephoneNumber"));
dt.Columns.Add(new DataColumn("Email1Address"));
dt.Columns.Add(new DataColumn("WebPage"));
dt.Columns.Add(new DataColumn("BusinessTelephoneNumber"));
dt.Columns.Add(new DataColumn("GovernmentIDNumber"));
dt.Columns.Add(new DataColumn("Suffix"));
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("MiddleName"));
dt.Columns.Add(new DataColumn("NickName"));
dt.Columns.Add(new DataColumn("Department"));
dt.Columns.Add(new DataColumn("FirstName"));
dt.Columns.Add(new DataColumn("JobTitle"));
dt.Columns.Add(new DataColumn("CompanyName"));
Outlook.ContactItem oCt;
for (int i = 1; i <= oItems.Count; i++)
{
//Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetFirst();
oCt = (Outlook.ContactItem)oItems[i];
if (oCt.Email1Address.ToString() == sender)
{
DataRow dr = dt.NewRow();
if (oCt.FullName != null)
{
dr["FullName"] = oCt.FullName.ToString().Replace("\'", "\'\'");
}
if (oCt.PagerNumber != null)
{
dr["PagerNumber"] = oCt.PagerNumber.ToString().Replace("\'", "\'\'");
}
if (oCt.MobileTelephoneNumber != null)
{
dr["MobileTelephoneNumber"] = oCt.MobileTelephoneNumber.ToString().Replace("\'", "\'\'");
}
if (oCt.Email1Address != null)
{
dr["Email1Address"] = oCt.Email1Address.ToString().Replace("\'", "\'\'");
}
if (oCt.WebPage != null)
{
dr["WebPage"] = oCt.WebPage.ToString().Replace("\'", "\'\'");
}
if (oCt.BusinessTelephoneNumber != null)
{
dr["BusinessTelephoneNumber"] = oCt.BusinessTelephoneNumber.ToString().Replace("\'", "\'\'");
}
if (oCt.GovernmentIDNumber != null)
{
dr["GovernmentIDNumber"] = oCt.GovernmentIDNumber.ToString().Replace("\'", "\'\'");
}
if (oCt.Suffix != null)
{
dr["Suffix"] = oCt.Suffix.ToString().Replace("\'", "\'\'");
}
if (oCt.LastName != null)
{
dr["LastName"] = oCt.LastName.ToString().Replace("\'", "\'\'");
}
if (oCt.MiddleName != null)
{
dr["MiddleName"] = oCt.MiddleName.ToString().Replace("\'", "\'\'");
}
if (oCt.NickName != null)
{
dr["NickName"] = oCt.NickName.ToString().Replace("\'", "\'\'");
}
if (oCt.Department != null)
{
dr["Department"] = oCt.Department.ToString().Replace("\'", "\'\'");
}
if (oCt.FirstName != null)
{
dr["FirstName"] = oCt.FirstName.ToString().Replace("\'", "\'\'");
}
if (oCt.JobTitle != null)
{
dr["JobTitle"] = oCt.JobTitle.ToString().Replace("\'", "\'\'");
}
if (oCt.CompanyName != null)
{
dr["CompanyName"] = oCt.CompanyName.ToString().Replace("\'", "\'\'");
}
dt.Rows.Add(dr);
}
}
//Show the item to pause.
//oCt.Display(true);
oNS.Logoff();
oApp = null;
oItems = null;
oContacts = null;
oNS = null;
oApp = null;
return dt;
}

No comments: