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

标签 c# email outlook imap inbox

如何使用 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;
}

关于c# - 如何使用 C# 访问 Outlook 帐户收件箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46480602/

相关文章:

c# - 游戏逻辑动态可扩展架构实现模式

PHP电子邮件风格

iphone - IOS 自定义图标电子邮件附件

c++ - 连接到 Outlook 事件

.net - 授予对 Outlook 应用程序的访问权限

c# - Outlook MAPI 存储提供程序。管理?

c# - 将程序定义为测试版

c# - Web API请求内容为空

c# - 算术运算导致溢出 MySql DB 连接打开

html - 电子邮件签名 - 标题横幅