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

No comments: