[英]Outlook RPC server unavailable after sending email through .Net app
我剛剛開始研究.Net與Office應用程序之間的互操作性,尤其是Outlook。 我設法使電子郵件通過Outlook發送,但是由於某種原因,發送電子郵件后Outlook的RPC服務器關閉了(如果Outlook尚未運行)。 這是我的代碼:
private void CreateEmailItem(string subjectEmail, string toEmail, string bodyEmail)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
try
{
Outlook.Inspector oInspector = oMsg.GetInspector;
oMsg.Subject = subjectEmail;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(toEmail);
oRecip.Resolve();
oMsg.HTMLBody = "<html><div style=font-size:16px; font-face:consolas;>" + bodyEmail + "</font></body></div></html>";
oMsg.Send();
}
catch (Exception e)
{ }
finally
{
//cleanup
Marshal.FinalReleaseComObject(oMsg);
oApp.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
因此,當我嘗試調用Outlook應用程序Quit()方法時會引發錯誤?
嘗試刪除Marshal.FinalReleaseComObject(oMsg); 代碼行。 如果使用GC.Collect方法,則無需釋放基礎的COM對象。
我也建議在全局范圍內聲明一個Application對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.