使用.NET 2.0和C#发送附件时出现错误 [英] Errors when sending attachments with .NET 2.0 and C#

查看:121
本文介绍了使用.NET 2.0和C#发送附件时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Excel文件作为附件发送时,出现以下错误.

System.Net.Mail.SmtpException:发送文件失败---> System.ObjectDisposedException:无法访问关闭的文件.
在System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
在System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
在System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult asyncResult)

并且文件未发送.

此错误是什么意思?

Frank

When sending a excel file as an attachment, I receive the following error.

System.Net.Mail.SmtpException: Failure sending file ---> System.ObjectDisposedException: Cannot access a closed file.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult asyncResult)

And the file is not sent.

What does this error mean?

Frank

推荐答案

无法访问已关闭的文件已有说明.您正在关闭流,然后再发送邮件.

如果仍然无法识别出来,请发布代码,然后一些代码将其指向.
Cannot access a closed file is already explanatory. You are closing the stream before the mail is sent.

If you still can not make it out, post your code and some one would pin point to it.


查看所引发的异常:ObjectDisposedException.我怀疑那是您的线索:您正在使用某些东西之前要对其进行处置.

查看在Manas Bhardwaj的答案中添加注释的代码,您在方法结尾处执行insMail.Dispose(),但是您正在使用异步方法发送邮件.这意味着您的方法可以(并且,我确实可以)在发送邮件之前返回.

如果您将对Dispose的调用移到回调例程(SMTPmail_SendCompleted)中,我希望您的问题会解决.
Look at the exception that was thrown: ObjectDisposedException. That, I suspect, is your clue: you are disposing something before it has been used.

Reviewing the code that you put in the comment to Manas Bhardwaj''s answer, you are doing insMail.Dispose() at the end of your method, but you are using an asynchronous method to send the mail. This means that your method can (and, I suspect, does) return before the mail is sent.

If you move the call to Dispose into the callback routine (SMTPmail_SendCompleted) I expect your problem will go away.


感谢您的帮助!我能够解决此问题.

弗兰克
Thanks for the help! I was able to correct the issue.

Frank


这篇关于使用.NET 2.0和C#发送附件时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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