C# 如果Outlook处于打开状态,则只能通过Outlook发送电子邮件
我想使用如上所述的通过Outlook发送电子邮件。只要我已经打开Outlook,它就可以正常工作。例如,如果Outlook最小化,我执行我的代码,那么我可以发送一封电子邮件。但如果Outlook已关闭,则会出现异常:C# 如果Outlook处于打开状态,则只能通过Outlook发送电子邮件,c#,outlook-2010,C#,Outlook 2010,我想使用如上所述的通过Outlook发送电子邮件。只要我已经打开Outlook,它就可以正常工作。例如,如果Outlook最小化,我执行我的代码,那么我可以发送一封电子邮件。但如果Outlook已关闭,则会出现异常: {System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)) at Microsoft.O
{System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
at Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()
at OutlookExample.Form1.btnSendEmail_Click(Object sender, EventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\OutlookExample\OutlookExample\Form1.cs:line 28}
代码如下:
using Outlook = Microsoft.Office.Interop.Outlook;
...
private void btnSendEmail_Click(object sender, EventArgs e)
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello, here is your message!";
oMsg.Subject = "This is a test message";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("rhr@sonlinc.dk");
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
为什么这样不行
编辑:这是解决方案
using Outlook = Microsoft.Office.Interop.Outlook;
...
private void btnSendEmail_Click(object sender, EventArgs e)
{
try
{
Outlook.Application oApp = new Outlook.Application();
// These 3 lines solved the problem
Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
System.Threading.Thread.Sleep(5000); // test
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello, here is your message!";
oMsg.Subject = "This is a test message";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("rhr@sonlinc.dk");
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
以下代码已可靠地为我工作了数月:
app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI");
f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Thread.Sleep(5000); // a bit of startup grace time.
如果outlook是打开的,它将使用它,如果不是打开的,它将使用它。当然,如果您的outlook要求您登录,您的代码将不允许这样做。有些系统使您很难自动登录。我不喜欢使用Thread.Sleep 5秒钟,因此我找到了另一种解决方案,对我来说很有效: 您只需获取新创建的MailItem的Inspector对象
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMsg.GetInspector;
答案最初是在中发布的,适用于Outlook 2007(但它在Outlook 2010中对我有效)不要使用Outlook。相反,请使用System.Net.Mail.Good提问。确定你还没有登录吗?我希望是SLaks。不幸的是,我正在维护VB6代码,只是在C#中复制了这个问题。但是,这个inspector对象有什么帮助呢?它没有在任何地方使用,或者我遗漏了什么?似乎Outlook只有在正确初始化后才返回Inspector。这就是诀窍。您不必使用它。那么,您是否检查它是否为
null
?或者,如果您试图获取inspector对象,而Outlook尚未启动,它只是强制Outlook初始化吗?据我记忆,它只是强制Outlook初始化。我没有检查它的价值,