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

如何使用c#访问outlook帐户收件箱?,c#,email,outlook,imap,inbox,C#,Email,Outlook,Imap,Inbox,如何使用c#访问outlook帐户收件箱?我尝试过使用outlook对象模型和imap,但它只显示登录到我的计算机的outlook帐户。我尝试使用用户名和密码登录到其他帐户,但它仍然登录到我的本地计算机帐户。我读到登出并关闭outlook应该可以解决这个问题,但它并没有改变结果 这是密码 using System; using System.Reflection; using Outlook = Microsoft.Office.Interop.Outlook; na

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

    }
}
使用系统;
运用系统反思;
使用Outlook=Microsoft.Office.Interop.Outlook;
命名空间控制台应用程序1
{
公共班级1
{
公共静态int Main(字符串[]args)
{
尝试
{
Outlook.Application oApp=新建Outlook.Application();
//获取MAPI命名空间。
Outlook.NameSpace-oNS=oApp.GetNamespace(“MAPI”);
登录(“电子邮件地址占位符”,“密码占位符”,false,true);
//获取收件箱文件夹。
Outlook.MAPIFolder oInbox=oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
字符串user=oNS.CurrentUser.EntryID;
//获取收件箱文件夹中的项目集合。
Outlook.Items oItems=oInbox.Items;
控制台写入线(oItems.Count);
Outlook.MailItem oMsg=(Outlook.MailItem)oItems.GetFirst();
Console.WriteLine(oMsg.Subject);
Console.WriteLine(oMsg.SenderName);
Console.WriteLine(oMsg.ReceivedTime);
控制台写入线(oMsg主体);
int AttachCnt=oMsg.Attachments.Count;
Console.WriteLine(“附件:+AttachCnt.ToString());
如果(附件>0)
{
对于(int i=1;i 0)
{

对于(int j=1;j您可以尝试使用以下代码:

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;
}
public List getmail()
{
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=命名空间.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.OlFolderInboxFolder);
列表电子邮件=新列表();
foreach(inboxFolder.Items中的Microsoft.Office.Interop.Outlook.MailItem MailItem)
{
如果(邮件项目.未读)
电子邮件。添加(邮件项);
}
回复邮件;
}

显示您尝试的代码的最低版本以及该代码的结果。该帐户是否已添加到Outlook?是的,我尝试将不同的帐户添加到Outlook,但它仍转到我的主电子邮件地址。我正在使用.Logon功能,我不完全确定是否有正确的用户名参数格式。是em吗邮件地址本身或其他内容?此外,代码如何与Outlook应用程序交互以确定谁是用户?我是否需要在Outlook端执行任何操作以允许访问收件箱?我可以访问两个具有相同凭据的不同电子邮件帐户。我可以访问其中一个,但如何访问第二个邮箱?以下代码适用于我。Outlook.MAPIFolder theInbox=oNS.Folders[“Mailbox-Name Here”]。Folders[“Inbox”];