【问题标题】:Reading e-mail without Outlook app open在未打开 Outlook 应用程序的情况下阅读电子邮件
【发布时间】:2016-02-27 06:38:38
【问题描述】:

这就是我使用 C# 阅读电子邮件的方法:

outLookApp.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx);
            Outlook.NameSpace olNameSpace = outLookApp.GetNamespace("mapi");

olNameSpace.Logon("xxxx", "xxxxx", false, true);
Outlook.MAPIFolder oInbox  = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items oItems  = oInbox.Items;
MessageBox.Show("Total : " + oItems.Count); //Total Itemin inbox
 oItems = oItems.Restrict("[Unread] = true");
 MessageBox.Show("Total Unread : " + oItems.Count); //Unread Items
 Outlook.MailItem oMsg;


 Outlook.Attachment mailAttachement;
 for (int i = 0; i < oItems.Count; i++)
 {
     oMsg = (Outlook.MailItem)oItems.GetFirst();

     MessageBox.Show(i.ToString());

    MessageBox.Show(oMsg.SenderName);
    MessageBox.Show(oMsg.Subject);
    MessageBox.Show(oMsg.ReceivedTime.ToString());
    MessageBox.Show(oMsg.Body);

我面临的问题是这个应用程序只有在机器上打开 Outlook 时才能工作。如果 Outlook 已关闭,则会引发异常:

服务器不可用。如果这种情况仍然存在,请联系您的管理员。

我是否可以在打开 Outlook 的情况下阅读电子邮件?

【问题讨论】:

    标签: c# email outlook


    【解决方案1】:

    当 Outlook 关闭时,您可能会遇到this

    同时关注this tutorial 将确保您执行所有正确的步骤。

    祝你好运!

    【讨论】:

      【解决方案2】:

      这是一个老问题,但我会回答这个问题,因为我在同一个问题上苦苦挣扎了很长时间,而此页面上以前的答案并没有真正帮助我。

      我必须编写一个程序并使用 Outlook 在具有不同 UAC 级别的不同机器上发送电子邮件,这是我经过很长时间后想出的。

      using Outlook = Microsoft.Office.Interop.Outlook;
      
      // Create the Outlook application.
      Outlook.Application oApp = null;
      
      // Check whether there is an Outlook process running.
      int outlookRunning = Process.GetProcessesByName("OUTLOOK").Length;
      if (outlookRunning > 0)
      {
          // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
          try
          {
              oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
          }
          catch (Exception)
          {
              oApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")) as Outlook.Application;
          }
          finally
          {
              // At this point we must kill Outlook (since outlook was started by user on a higher prio level than this current application)
              // kill Outlook (otherwise it will only work if UAC is disabled)
              // this is really a kind of last resort
              Process[] workers = Process.GetProcessesByName("OUTLOOk");
              foreach (Process worker in workers)
              {
                  worker.Kill();
                  worker.WaitForExit();
                  worker.Dispose();
              }
          }
      }
      else
      {
          // If not, create a new instance of Outlook and log on to the default profile.
          oApp = new Outlook.Application();
          Outlook.NameSpace nameSpace = oApp.GetNamespace("MAPI");
          try
          {
              // use default profile and DO NOT pop up a window
              // on some pc bill gates fails to login without the popup, then we must pop up and lets use choose profile and allow access
              nameSpace.Logon("", "", false, Missing.Value);
          }
          catch (Exception)
          {
              // use default profile and DO pop up a window
              nameSpace.Logon("", "", true, true);
          }
          nameSpace = null;
      }
      
      // Done, now you can do what ever you want with the oApp, like creating a message and send it
      // Create a new mail item.
      Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
      

      【讨论】:

        【解决方案3】:

        您确定要将 Outlook 用作代理吗?

        peopleseems 在 C# 中处理此类任务的低级别(令人惊讶的是框架中没有任何内置组件...)

        关于 Mat 的回应,Redemption 确实是一个很好的产品(在 Outlook 中使用它来解析邮件),但我怀疑它是否可以在没有运行 Outlook 的情况下工作。

        【讨论】:

          【解决方案4】:

          我个人不会使用 Outlook 作为代理。如果您试图最终监控 Exchange 存储,那么我会使用 WebDav。您的 Exchange 服务器必须支持它 - 但如果支持,它就是一个简单的 XML API。好吧,API 位很简单,但 XML 相当复杂。但是,一旦您将其封装在一些代码中,使用起来就轻而易举了。

          【讨论】:

          • 我认为这与问题无关。
          • 塞缪尔,我的回答是对“不,你不能”的详细说明。我认为它会更有帮助!
          【解决方案5】:

          使用 MAPI 客户端检索电子邮件并使用 MIME 解码器读取它们。两者都存在于 lumisoft 框架中:

          http://www.lumisoft.ee/lswww/download/downloads/Net/

          【讨论】:

            【解决方案6】:

            为您的代码使用Redemption COM library

            【讨论】:

              猜你喜欢
              • 2021-05-19
              • 1970-01-01
              • 1970-01-01
              • 2012-02-04
              • 1970-01-01
              • 2011-04-18
              • 2021-12-15
              • 1970-01-01
              • 2017-09-05
              相关资源
              最近更新 更多