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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/301882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:19:35  来源:igfitidea点击:

Reading e-mail without Outlook app open

c#emailoutlook

提问by

Thats what I am using to read e-mail using C#:

这就是我使用 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);

The problem that I am facing is this application only works if the Outlook is open on the machine. If Outlook is closed it throws an exception:

我面临的问题是此应用程序仅在机器上打开 Outlook 时才有效。如果 Outlook 已关闭,则会引发异常:

The server is not available. Contact your administrator if this condition persists.

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

Is there anyway I can read e-mail with Outlook open?

无论如何,我可以在打开 Outlook 的情况下阅读电子邮件吗?

回答by Mat Nadrofsky

You'll likely run into thiswhen Outlook is closed.

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

Also following this tutorialwill ensure you're doing all the right steps part and parcel.

此外,遵循本教程将确保您执行所有正确的步骤。

Best of luck!

祝你好运!

回答by Vinzz

Are you sure you want to use Outlook as a proxy?

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

peopleseemsto deal low level with such a task in C# (surprising there isn't any built-in component in the framework...)

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

Concerning Mat's response, Redemption is indeed a fine product (used it to parse mails upon arrival in outlook), but I doubt it can work without outlook running.

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

回答by Steve Dunn

I would personally not use Outlook as a proxy. If you're trying to ultimately monitor an Exchange store, then I'd use WebDav. Your Exchange server must support it - but if it does, it's a simple XML API. Well, the API bit is simple, but the XML is quite convoluted. But once you've encapsulated this in a bit of code, it's a doddle to use.

我个人不会使用 Outlook 作为代理。如果您尝试最终监控 Exchange 商店,那么我会使用 WebDav。您的 Exchange 服务器必须支持它 - 但如果支持,它就是一个简单的 XML API。嗯,API 位很简单,但 XML 相当复杂。但是一旦你把它封装在一些代码中,使用起来就很容易了。

回答by Steve Dunn

Use the Redemption COM libraryfor your code.

为您的代码使用Redemption COM 库

回答by jgauffin

Use a MAPI client to retrieve the emails and a MIME decoder to read them. Both exists in the lumisoft framework:

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

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

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

回答by theAlse

This is kind of an old question, but I am going to answer it since I struggled with the same issue for a long time and the previous answers on this page did not really help me.

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

I had to write a program and use outlook to send an email on different machines with different UAC-levels and this is what I came up with after a long time.

我不得不编写一个程序并使用 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);