Why can't I get ALL MailItems files using interop outlook?

I am trying to use Microsoft.Office.Interop.Outlook to receive emails from my Outlook mailbox. 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 shows only one email. There should be three of them. The email he shows is the oldest of them ... why don't I get all three? Is there a mailing address other than MailItem that will be in my inbox?

+4
source share
1 answer

- - List<MailItem> . , , .

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
}
+6

Source: https://habr.com/ru/post/1527026/


All Articles