发送电子邮件时的COM异常 [英] COM exception when sending email

查看:84
本文介绍了发送电子邮件时的COM异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试使用以下代码在我的WPF应用中发送电子邮件。我呼叫有任何未处理的电子邮件


  private static void ApplicationDispatcherUnhandledException(object sender,DispatcherUnhandledExceptionEventArgs e)

        {

//在此处记录例外详情:            

e .Handled = true;


            SendEmail();

        }


private void SendEmail()

{

try


{

app = new Outlook.Application();


ns = app.GetNamespace(" mapi");


ns。登录(Missing.Value,Missing.Value,true,true);


msg =(Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);


...

...

...


await Task.Run(()=> ; $
                {

                    (msg as Outlook._MailItem).Send();

                    AppLogger.Log.Info( "电子邮件已成功发送!";);
               } ); $
}


catch(Exception ex)

{

//处理例外。

}






            {

                if(ns!= null)

                {

                    ns.Logoff();&
                }


                msg = null;&
                ns = null;&
                app = null;

            } b $ b}


此代码似乎工作正常,并且在其他环境中部署时会收到电子邮件。

但是今天,当我通过Visua Studio在我的机器上运行代码,我在此行收到以下错误 - >(msg as Outlook._MailItem).Send();:


异常:[ System.Runtime.InteropServices.InvalidComObjectException:无法使用已与其基础RCW分离的COM对象。 COM对象在另一个线程上仍在使用时被释放。


想知道,为什么我会收到此错误以及如何解决?


感谢您的帮助。

解决方案

异常:[System.Runtime.InteropServices.InvalidComObjectException :无法使用已与其基础RCW分离的COM对象。 COM对象在另一个线程上仍在使用时被释放。


似乎解决方案不是安全的,因为不是托管代码中的COM对象由.NET CLR管理。


此外,您可能需要查看RCW。


https://docs.microsoft.com/en-us/dotnet/framework/interop/how-to-create-wrappers-手动


Hello,

I'm trying to send email in my WPF app using following code.I call wheneve there is any unhandled e

 private static void ApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
//Log the exception details here:            
e.Handled = true;

            SendEmail();
        }

private void SendEmail()
{
try

{
app = new Outlook.Application();

ns = app.GetNamespace("mapi");

ns.Logon(Missing.Value, Missing.Value, true, true);

msg = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);

...
...
...

await Task.Run(() =>
                {
                    (msg as Outlook._MailItem).Send();
                    AppLogger.Log.Info("Email sent successfully!.");
                });
}

catch(Exception ex)
{
//Handle exception here.
}


finally
            {
                if (ns != null)
                {
                    ns.Logoff();
                }

                msg = null;
                ns = null;
                app = null;
            }
}

This code seems to work fine and emails are getting sent when deployed in other environments.
But today,when I was running code on my machine through Visua Studio,I got the following error at this line-->(msg as Outlook._MailItem).Send();:

Exception: [System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used. The COM object was released while it was still in use on another thread.

Wondering, why did I get this error and how do I resolve?

Thanks for any help.

解决方案

Exception: [System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used. The COM object was released while it was still in use on another thread.

It don't seem that the solution is tread safe, since a COM object in not managed code managed by the .NET CLR.

Also, you may want to look at RCW.

https://docs.microsoft.com/en-us/dotnet/framework/interop/how-to-create-wrappers-manually


这篇关于发送电子邮件时的COM异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆