9

这就是我使用 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 的情况下阅读电子邮件吗?

4

6 回答 6

2

当 Outlook 关闭时,您可能会遇到这种情况。

同样遵循本教程将确保您执行所有正确的步骤。

祝你好运!

于 2008-11-19T13:31:52.380 回答
1

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

我必须编写一个程序并使用 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);
于 2014-03-19T09:52:50.623 回答
0

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

人们 似乎在 C# 中处理这样一个任务的低级别(令人惊讶的是框架中没有任何内置组件......)

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

于 2008-11-19T14:05:22.860 回答
0

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

于 2008-11-19T14:42:22.520 回答
0

为您的代码使用Redemption COM 库

于 2009-05-01T04:06:45.090 回答
0

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

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

于 2012-02-21T20:54:51.440 回答