如何使用c#访问Outlook帐户收件箱?

时间:2017-09-29 01:49:11

标签: c# email outlook imap inbox

如何使用c#访问Outlook帐户收件箱?我尝试过使用outlook对象模型和imap,但它只显示登录到我机器的Outlook帐户。我尝试使用其用户名和密码登录到另一个帐户,但它仍然登录到我的本地计算机帐户。我读到注销和关闭前景应该解决这个问题,但它并没有改变结果

这是代码

   using System;
   using System.Reflection;

   using Outlook = Microsoft.Office.Interop.Outlook;

   namespace ConsoleApplication1
   {
       public class Class1
       {
           public static int Main(string[] args)
           {
               try
               {

                   Outlook.Application oApp = new Outlook.Application();

                   // Get the MAPI namespace.
                   Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");             
                   oNS.Logon("email address placeholder", "password placeholder", false, true);

                   //Get the Inbox folder.
                   Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                   String user = oNS.CurrentUser.EntryID;

                   //Get the Items collection in the Inbox folder.
                   Outlook.Items oItems = oInbox.Items;

                   Console.WriteLine(oItems.Count);               
                   Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();

                   Console.WriteLine(oMsg.Subject);
                   Console.WriteLine(oMsg.SenderName);
                   Console.WriteLine(oMsg.ReceivedTime);
                   Console.WriteLine(oMsg.Body);

                   int AttachCnt = oMsg.Attachments.Count;
                   Console.WriteLine("Attachments: " + AttachCnt.ToString());

                   if (AttachCnt > 0) 
                   {
                   for (int i = 1; i <= AttachCnt; i++) 
                    Console.WriteLine(i.ToString() + "-" + oMsg.Attachments[i].DisplayName);
                   }

            for (int i = 0; i < oItems.Count; i++)
            {
                if (oItems.GetNext() is Outlook.MailItem)
                {
                    oMsg = (Outlook.MailItem)oItems.GetNext();
                    Console.WriteLine(oMsg.Subject);
                    Console.WriteLine(oMsg.SenderName);
                    Console.WriteLine(oMsg.ReceivedTime);
                    Console.WriteLine(oMsg.Body);
                    AttachCnt = oMsg.Attachments.Count;
                    if (AttachCnt > 0)
                    {
                        for (int j = 1; j <= AttachCnt; j++)
                            Console.WriteLine(j.ToString() + "-" + oMsg.Attachments[j].DisplayName);
                    }

                    Console.WriteLine("Attachments: " + AttachCnt.ToString());
                    Console.WriteLine("CURRENT EMAIL # IS: " + i);
                }
               else
                {
                    oItems.GetNext();
                    Console.WriteLine("NOT AN EMAIL");
                    Console.WriteLine("CURRENT EMAIL # IS: " + i);
                }


            }

            oNS.Logoff();

            oMsg = null;
            oItems = null;
            oInbox = null;
            oNS = null;
            oApp = null;
        }

        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught: ", e);


        }

        return 0;

    }
}

}

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码:

public List<Outlook.MailItem> GetMails()
{
  Microsoft,Office.Interop.Outlook.NameSpace nameSpace = OutLookName.Application.GetNameSpace("MAPI");
  nameSpace.Logon("","",System.Reflection.Missing.Value,System.Reflection.Missing.Value);
  Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
  List<Outlook.MailItem> eMails = new List<Outlook.MailItem>();

  foreach(Microsoft.Office.Interop.Outlook.MailItem mailItem in inboxFolder.Items)
  {
    if(mailItem.UnRead)
      eMails.Add(mailItem);
  }
return eMails;
}