簡體   English   中英

如何使用C#訪問Outlook帳戶收件箱?

[英]How do i access an outlook account inbox using c#?

如何使用C#訪問Outlook帳戶收件箱? 我嘗試使用Outlook對象模型和imap,但它僅顯示登錄到我的計算機中的Outlook帳戶。 我嘗試使用其用戶名和密碼登錄到其他帳戶,但仍登錄到本地計算機帳戶。 我讀到注銷並關閉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;

    }
}

}

您可以嘗試使用以下代碼:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM