【发布时间】:2019-08-14 07:44:31
【问题描述】:
我正在用 C# 构建一个 ASP webforms 应用程序,我正在尝试启动 Outlook 以从客户端计算机发送电子邮件。
我正在使用以下示例,这是我在网上找到的。
public void sendEmail(object sender, EventArgs e)
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the MAPI namespace.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using the default profile or existing session (no dialog box).
oNS.Logon(Missing.Value, Missing.Value, false, true);
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
oMsg.HTMLBody = "Test";
//Subject line
oMsg.Subject = "Test Subject";
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip;
oRecip = (Outlook.Recipient)oRecips.Add("yyyyy@mydomain.co.uk");
oRecip.Resolve();
// Send.
oMsg.Send();
//Log off.
oNS.Logoff();
}
我在这行代码中遇到问题:
oNS.Logon(Missing.Value, Missing.Value, false, true);
即使我配置了 Outlook 配置文件,当它运行这行代码时,我的应用也会启动 Outlook 并显示“欢迎使用 Outlook 2016”对话框。
预期的行为是它使用现有配置文件启动 Outlook。
如果 Outlook 已经打开,我会收到一条错误消息:
System.Runtime.InteropServices.COMException: '正在检索 COM 类 具有 CLSID 的组件的工厂 {0006F03A-0000-0000-C000-000000000046} 由于以下原因而失败 错误:80080005 服务器执行失败(来自 HRESULT 的异常: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))。'
如果 Outlook 已打开,则预期行为是让我的应用程序使用现有的 Outlook 进程。
非常感谢任何帮助。
【问题讨论】:
标签: c# asp.net outlook office-interop