Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im unable to make this. Could please check with this.

I'm looking for the process in C# where I give email id and password of any outlook and to display all unread emails from Outlook.

Example:

Email id : xyz@example.com
Password : xyzpassword

When I give this email id I need all unread email from that account xyz@abc.com


Email id : rts@example.com
Password : rtspassword

When I give this email id I need all unread email from that account rts@abc.com

This is what I'm trying:

I want to give username and password of the Outlook email at the runtime and I would like to access the unread mails of that emails. This works out when I login to my outlook account, but it should be able to access any account of Outlook based on user id and password.

This code is reading only the first email from outlook but not the unread mail and its not taking user id and password at the runtime.


private void btnAccessEmail_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass( );
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");


Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.In terop.Outlook.OlDefaultFolders.olFolderContacts);

//login
mapiNameSpace.Logon(null, null, false,false);

mapiNameSpace.Logon("my Emailid", "my emailid password", Missing.Value, true);


Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;

Console.WriteLine("Total : ", myItems.Count);

Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.In terop.Outlook.OlDefaultFolders.olFolderInbox);

Console.Write(myInbox.Name);

Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;

Console.WriteLine("Total : ", inboxItems.Count);

Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);

mapiNameSpace.Logon("Profile", Missing.Value, false, true);



if (myInbox.Items.Count > 0)
{


Microsoft.Office.Interop.Outlook.Items items = myInbox.Items;
inboxItems = inboxItems.Restrict("[UnRead] = true");
Console.WriteLine("Total Unread :", inboxItems.Count);




//Int32 cnt = 0;


//try
//{
// foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
// {
// Console.WriteLine(mail.Subject);
// cnt++;
// if (mail.UnRead == true)
// {
// }

// }
//}

//catch
//{

//}

//Console.WriteLine(cnt);


// Grab the Subject
lblSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbo x.Items[1]).Subject;


// Grab the Body
txtBody.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbo x.Items[1]).Body;

// Sender Name
lblSenderName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbo x.Items[1]).SenderName;

// Sender Email
lblSenderEmail.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbo x.Items[1]).SenderEmailAddress;

// Creation date
lblCreationdate.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbo x.Items[1]).CreationTime.ToString();
}
else
{
MessageBox.Show("There are no emails in your Inbox.");
}
}
}
Posted
Comments
jo.him1988 9-Jul-14 8:42am    
your code is ok try this hope it will work for you :)

1 solution

Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
           Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
           Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

           //login
           mapiNameSpace.Logon(null, null, false, false);

           mapiNameSpace.Logon("yourMailAddress", "password", Missing.Value, true);


           Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;

          // Console.WriteLine("Total : ", myItems.Count);

           Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

         //  Console.Write(myInbox.Name);

           Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;

          // Console.WriteLine("Total : ", inboxItems.Count);

           Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);

           mapiNameSpace.Logon("Profile", Missing.Value, false, true);



           if (myInbox.Items.Count > 0)
           {
               Microsoft.Office.Interop.Outlook.Items items = myInbox.Items;
               inboxItems = inboxItems.Restrict("[UnRead] = true");
               Console.WriteLine(string.Format("Total Unread message {0}:", inboxItems.Count));
               int x=0;
               foreach (Microsoft.Office.Interop.Outlook.MailItem item in inboxItems)
               {
                   Console.WriteLine("{0} unread mail from your inbox", ++x);
                   Console.WriteLine(string.Format("from:-       {0}", item.SenderName));
                   Console.WriteLine(string.Format("To:-         {0}", item.ReceivedByName));
                   Console.WriteLine(string.Format("Subject:-     {0}", item.Subject));
                   Console.WriteLine(string.Format("Message:-     {0}", item.Body));
                   System.Threading.Thread.Sleep(1000);
               }
               Console.ReadKey();


happy coding :) :) :)
 
Share this answer
 
v5
Comments
Lassi91 11-Jul-14 4:51am    
Its working :)
thank u :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900