Wednesday, June 4, 2008

reading un read messages from outlook in .net

http://msdn.microsoft.com/en-us/library/ms268996(VS.80).aspx



private void button1_Click(object sender, EventArgs e)
{
Outlook.Application oApp = new Outlook.Application();
// String used for comparison with mail item.
object sClassComp = "IPM.Note";
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items oItems = oInbox.Items;
label1.Text="Total : " + oItems.Count;
// Get unread e-mail messages.
oItems = oItems.Restrict("[Unread] = true");
label2.Text = "Total Unread : " + oItems.Count;
Outlook.MailItem oMsg;
Outlook.MailItem[] mailitems = new Microsoft.Office.Interop.Outlook.MailItem[oItems.Count];
//oItems.Application.
int i=0;
string str = "";
foreach (object o in oItems)
{
mailitems[i] = (Outlook.MailItem)o;
i = i + 1;
}
foreach(Outlook.MailItem o in mailitems)
{
//Test to make sure item is a mail item
//and not a meeting request.
if (o != null)
{
if (string.Compare(o.MessageClass.ToString(),sClassComp.ToString(),true)==0)
{
str =(string)o.Subject;
}
}
//oMsg = oItems.Item[i];
// str+=oMsg.Subject;
//Console.WriteLine(oMsg.ReceivedTime);
//Console.WriteLine(oMsg.Body);
//Console.WriteLine("---------------------------");

//Console.WriteLine(i);
//Console.WriteLine(oMsg.SenderName);
//Console.WriteLine(oMsg.Subject);
//Console.WriteLine(oMsg.ReceivedTime);
//Console.WriteLine(oMsg.Body);
//Console.WriteLine("---------------------------");
}
// Clean up.
label3.Text = str;
oApp = null;
oNS = null;
oItems = null;
oMsg = null;
}
}

No comments: