Microsoft.Office.Interop.Outlook 确认电子邮件在 Outlook 打开时不起作用



我正在尝试在用户提交表单时发送确认电子邮件。我在使用smtp时遇到了隐私问题,所以我正在尝试通过Outlook来做到这一点。当 Outlook 未打开但只是挂起时,代码可以正常工作,在此处输入链接说明。任何想法如何解决这个问题或解决方法?

try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody. 
//add the body of the email
oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
//Subject line
oMsg.Subject = "Your Subject will go here.";
 // Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("email@address.com");
oRecip.Resolve();
// Send.
((Outlook._MailItem)oMsg).Send();

// Clean up.
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oMsg);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oRecip);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oRecips);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp);
}//end of try block
catch (Exception ex)
{
}//end of catch

尝试检查是否有 Outlook 实例已打开,如果有,请使用它发送邮件。如果没有,则创建新实例。

(未经测试)这应该可以帮助您入门:

Outlook.Application oApp;
try { 
    oApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application"); 
}
catch () {
    oApp = new Outlook.Application();
}
// use oApp to send the stuff, it will either be the existing instance or a new instance

最新更新