Thursday, June 4, 2009

Retrieve Mail Items/ Email form outlook in C#.net

public void RetrieveEmailDetails(object oItem, string strMessageClass, string TypeofTrack)
{
Outlook.MailItem oMailItem;
try
{
if (strMessageClass == "MailItem")
{
oMailItem = (Outlook.MailItem)oItem; // Mail Entry.
bool flag = false;
for (int i = 0; i < dt_Mail.Rows.Count; i++)
{
if (dt_Mail.Rows[i]["EntryID"].ToString() == oMailItem.EntryID.ToString())
{
flag = true;
break;
}
}
if (flag != true)
{
if (! String.IsNullOrEmpty(oMailItem.Subject.ToString()))
{
DataRow dr = dt_Mail.NewRow();
dr["Type"] = "Email";

if (oMailItem.Subject != null)
{
dr["Subject"] = oMailItem.Subject.ToString();
}
if (oMailItem.Body != null)
{
dr["Body"] = oMailItem.Body.ToString();
}
if (oMailItem.SenderEmailAddress != null)
{
dr["SenderEmailAddress"] = oMailItem.SenderEmailAddress.ToString();
}
if (oMailItem.SenderName != null)
{
dr["SenderName"] = oMailItem.SenderName.ToString();
}
if (oMailItem.To != null)
{
dr["To"] = oMailItem.To.ToString();
}
if (oMailItem.CC != null)
{
dr["CC"] = oMailItem.CC.ToString();
}
if (oMailItem.BCC != null)
{
dr["BCC"] = oMailItem.BCC.ToString();
}
if (oMailItem.Subject != null)
{
dr["ReceivedTime"] = oMailItem.ReceivedTime.ToString();
}
if (oMailItem.SentOn != null)
{
dr["SentOn"] = oMailItem.SentOn.ToString();
}
if (oMailItem.EntryID != null)
{
dr["EntryID"] = oMailItem.EntryID.ToString();
}
// string c = oMailItem.Recipients[1].Address.ToString();
dt_Mail.Rows.Add(dr);
btnSaveToDatabase.Enabled = true;
}
}
}

}
catch
{
label1.Text = "Please Select Proper Mail Item.";
btnSaveToDatabase.Enabled = false;
}
}

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;
}

Attach Notes to Contact / Account in CRM


public Guid InsertNotesForContact(string contactid, string body)
{
CrmService service = new CrmService();
service = MyService();
annotation note = new annotation();
//#2
//note.ownerid = new Owner();
//note.ownerid.type = "systemuser";
//note.ownerid.Value = new Guid("000-000-0000"); <-- don't use this value
//note.ownerid.name = "Administrator";
//#3
note.objectid = new Lookup();
// note.objectid.name = "contact name";
note.objectid.type = EntityName.contact.ToString();
note.objectid.Value = new Guid(contactid);
note.notetext = body;
//note.subject = "my attachment";
//#4
note.objecttypecode = new EntityNameReference();
note.objecttypecode.Value = EntityName.contact.ToString();
Guid newNoteID = service.Create(note);
return newNoteID;
}

Attach Notes to Contact / Account in CRM

public Guid InsertNotesForContact(string contactid, string body)
{
CrmService service = new CrmService();
service = MyService();
annotation note = new annotation();
//#2
//note.ownerid = new Owner();
//note.ownerid.type = "systemuser";
//note.ownerid.Value = new Guid("000-000-0000"); <-- don't use this value
//note.ownerid.name = "Administrator";
//#3
note.objectid = new Lookup();
// note.objectid.name = "contact name";
note.objectid.type = EntityName.contact.ToString();
note.objectid.Value = new Guid(contactid);
note.notetext = body;
//note.subject = "my attachment";
//#4
note.objecttypecode = new EntityNameReference();
note.objecttypecode.Value = EntityName.contact.ToString();
Guid newNoteID = service.Create(note);
return newNoteID;
}