Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I retrieve ALL MailItems using interop outlook?

I'm trying to use Microsoft.Office.Interop.Outlook to retrieve emails from my Outlook inbox. This is my code:

  Application app = new Application();
  NameSpace ns = app.Session;
  MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
  Items items = inbox.Items;
  foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
        {
            if (mail as MailItem != null)
            {
                Console.WriteLine(mail.Subject.ToString());
                Console.WriteLine(mail.Body.ToString());
                Console.ReadKey();
             }
        }

When I do this, it works--sort of. It only shows one email. There should be three. The email it's showing is the oldest one in there... why wouldn't I be able to get all three? Is there some other type of mail besides MailItem that would be in my inbox?

like image 258
eddie_cat Avatar asked Feb 14 '14 21:02

eddie_cat


People also ask

Why can't I See my IMAP email messages in outlook?

When you export email from an IMAP account in Outlook to a .pst file, you may not see the messages after you import the file back into Outlook. Note: The email messages aren't lost, but they are hidden from view.

How to fix outlook not showing emails in Inbox?

Now, go to Outlook inbox and click Send/Receive option to check if you are receiving the emails. 3. Disable the Auto Archive Settings If you’ve enabled the Auto Archive option, it may also cause the Outlook not showing emails in Inbox issue. To disable this option, follow the below-mentioned steps:

How to retrieve a message from the Outlook inbox using Visual C#?

To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to retrieve a message from the Inbox by using Visual C#, follow these steps: In Microsoft Visual Studio .NET or Visual Studio 2005, create a new Console Application project: On the File menu, point to New, and then select Project.

Why am I not receiving emails on my computer?

Check the Internet Connection If your internet connection is not stable, you may not receive the emails on time. A stable internet connection is a must to ensure that you receive emails in your Outlook. You can disconnect and reconnect your internet connection and see if it is the reason behind the issue.


1 Answers

I had this same exact problem - My workaround was just to create a List<MailItem> and loop through that. Make sure the emails aren't in subfolders though, otherwise they won't be found.

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

List<MailItem> ReceivedEmail = new List<MailItem>(); 
foreach (Outlook.MailItem mail in emailFolder.Items)               
    ReceivedEmail.Add(mail);

foreach (MailItem mail in ReceivedEmail)
{
    //do stuff
}
like image 191
ovaltein Avatar answered Nov 14 '22 22:11

ovaltein