我正在尝试在用户提交表单时发送确认电子邮件。我有smtp的私有问题所以我试图通过Outlook来做到这一点。当Outlook未打开时,代码工作正常enter link description here,但只是挂起。任何想法如何解决这个或周围的方法?
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
答案 0 :(得分:1)
尝试检查是否已打开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