在未打开Outlook应用程序的情况下阅读电子邮件

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打开的情况下读取电子邮件?
6个回答

2
您可能会在Outlook关闭时遇到这个问题
此外,遵循这个教程将确保您正确地执行所有步骤。
祝好运!

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

0

您确定要使用Outlook作为代理吗?

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

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


0

我个人不会使用Outlook作为代理。如果您最终要监视Exchange存储,则应使用WebDav。您的Exchange服务器必须支持它-但是如果支持,它就是一个简单的XML API。好吧,API部分很简单,但XML相当复杂。但是一旦您将其封装在一些代码中,使用起来就非常容易。


我认为这与问题无关。 - Samuel
Samuel,我的回答是对“不行”的阐述。我认为这会更有帮助! - Steve Dunn

0

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接