简体   繁体   English

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

[英]Microsoft.Office.Interop.Outlook Confirmation email doesn't work when Outlook is open

I am trying to send a confirmation email when a user submits a form. 我正在尝试在用户提交表单时发送确认电子邮件。 I had privlage problems with smtp so I am trying to do it through Outlook. 我在smtp上遇到特权问题,因此我正在尝试通过Outlook进行。 The code works fine enter link description here when Outlook isn't open but just hangs when it is. 如果Outlook未打开,但在挂起时却挂起,代码可以在此处很好地输入链接描述 Any ideas how to fix this or ways around it? 有什么想法可以解决此问题或解决方法吗?

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

Try checking to see if there is an instance of Outlook already open, if there is one use it to send the mail. 尝试检查是否有一个Outlook实例已经打开,是否有一个实例可以用来发送邮件。 If not then create the new instance. 如果不是,则创建新实例。

(Untested) this should get you started: (未经测试)这应该可以帮助您入门:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM