这就是我用来阅读使用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打开的情况下阅读电子邮件吗?
答案 0 :(得分:2)
答案 1 :(得分: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);
答案 2 :(得分:0)
您确定要将Outlook用作代理吗?
people seems在C#中处理此类任务的低级别(令人惊讶的是框架中没有任何内置组件......)
关于Mat的回应,Redemption确实是一个很好的产品(用它来解析邮件到达时的前景),但我怀疑它可以在没有前景运行的情况下工作。
答案 3 :(得分:0)
我个人不会将Outlook用作代理。如果您正在尝试最终监视Exchange存储,那么我将使用WebDav。您的Exchange服务器必须支持它 - 但如果确实如此,则它是一个简单的XML API。嗯,API位很简单,但XML非常复杂。但是一旦你将它封装在一些代码中,它就是一个轻而易举的事情。
答案 4 :(得分:0)
答案 5 :(得分:0)
使用MAPI客户端检索电子邮件和MIME解码器以读取它们。两者都存在于lumisoft框架中: