我在C#应用程序中使用非常简单的代码使用Outlook生成电子邮件。在Outlook已打开的情况下,它可以正常工作。(即使它请求打开电子邮件的权限。但是,在我授予访问权限后,会打开一个新的Outlook消息窗口,其中包含生成的电子邮件)。但是真正的问题是,如果未打开Outlook,应用程序将在此行崩溃
Microsoft.Office.Interop.Outlook.Recipient oTORecipt = oMsg.Recipients.Add(email);
错误:类型为'System.Runtime.InteropServices.COMException'的异常(第一次机会)发生在Birthday_Mailing.dll中。
附加信息:终止的操作(HRESULT的异常:0x80004004(E_ABORT))
如果有处理程序可用于此异常,则程序可以继续安全运行。
我的整个代码如下所示
public void SendMail(string email, string name)
{
//// Create the Outlook application by using inline initialization.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
////Create the new message by using the simplest approach.
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Microsoft.Office.Interop.Outlook.Recipient oTORecipt = oMsg.Recipients.Add(email); //oRecips.Add(t);
oTORecipt.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo;
oTORecipt.Resolve();
oMsg.Subject = "Herzlichen Glückwunsch";
oMsg.HTMLBody = "<html>" +
"<head>" +
"<title>Happy Birthday</title>" +
"</head>" +
"<body style='background-color:#E6E6E6;'>" +SelectImage+
"<div style='font-family: Georgia, Arial; font-size:14px; '>Liebe/r " + " " +
name + ",<br /><br />" +
"alles Gute zum <b>Geburtstag!</b> Wir wünschen einen schönen Tag und für das kommende Jahr Gesundheit, Glück und Erfolg." +
"<br /><br /><br /><br /><br />" +
"Mit den besten Grüßen,<br />" +
"XXYYZZ" +
"</div>" +
"</body>" +
"</html>";
oMsg.Save();
oMsg.Display();
oMsg = null;
oApp = null;
我不是编码专家。有人可以帮助我找到实际问题在哪里吗?提前致谢!
Microsoft.Office.Interop.Outlook.Inspector oInspector = oMsg.GetInspector;
我在声明可解决问题的oMsg对象后添加了这一行。但是仍在消息窗口打开之前,Outlook要求用户显示电子邮件的权限。
***但是,我不会将其标记为“答案”,因为这不是您可以为该问题提供的最佳解决方案。
更新:我发现上述问题是由于服务器中的某些安全系统引起的。这是正确的答案。感谢@MikeMiller显示正确的路径
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句