Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过.Net应用发送电子邮件后Outlook RPC服务器不可用_C#_Email_Outlook - Fatal编程技术网

C# 通过.Net应用发送电子邮件后Outlook RPC服务器不可用

C# 通过.Net应用发送电子邮件后Outlook RPC服务器不可用,c#,email,outlook,C#,Email,Outlook,我刚刚开始研究.Net和Office应用程序之间的互操作性,特别是Outlook。我已设法通过Outlook发送电子邮件,但由于某些原因,如果Outlook尚未运行,则在我发送电子邮件后,Outlook的RPC服务器将关闭。这是我的密码: private void CreateEmailItem(string subjectEmail, string toEmail, string bodyEmail) { Outlook.Application oApp = new Outlook.A

我刚刚开始研究.Net和Office应用程序之间的互操作性,特别是Outlook。我已设法通过Outlook发送电子邮件,但由于某些原因,如果Outlook尚未运行,则在我发送电子邮件后,Outlook的RPC服务器将关闭。这是我的密码:

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应用程序退出方法时引发错误?

尝试删除Marshal.finalreleasecomobjectomg;代码行。如果使用GC.Collect方法,则无需释放底层COM对象


我还建议在全局范围内声明应用程序对象

尝试全局声明应用程序,但在发送电子邮件时它仍会关闭。是否尝试删除FinalEleaseComObject调用?是的,我也删除了该调用,但问题是,outlook应用程序在oMsg.Send;被称为