CDO消息未发送且消息的附件不可随意删除

发布于 2024-09-14 12:59:32 字数 478 浏览 14 评论 0原文

我正在尝试使用 CDO 对象发送带有附件的消息。当 SMTP 服务器可用且所有信息正确时,邮件将与附件一起正确发送。

但是,如果 SMTP 服务器不正确,则消息不会发送(按预期),但它似乎“卡在”某处。我正在使用:

Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2

我在网上搜索过,发现这个选项会给出60秒的超时时间。但我附加到邮件中的文件永远不可用。

我所做的测试是使用无效的 SMTP 服务器发送带有附件的邮件。然后,我等待几分钟并尝试删除我附加的文件。但是,当我尝试这样做时,我遇到了权限问题。当我终止发送电子邮件程序时,我可以删除该文件。

我想知道如何配置超时以确保它放弃发送消息,如何在未发送消息时“分离”文件以及如何使程序等待消息发送(我想发送消息,然后从计算机中删除附加文件,因此我需要知道消息何时真正发送或何时超时)。

I am trying to send a message with attachment using CDO object. When the SMTP Server is available and all the information is correct, the message is correctly sent with the attachment.

However, if the SMTP Server is incorrect the message is not sent (as expected), but it seems to be "stuck" somewhere. I am using:

Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2

I've searched over the Internet, and found that this option would give a 60 second timeout. But the file I attached to the message is never available.

The test that I've done is to send a message with an attached file and using an invalid SMTP Server. Then, I wait for a few minutes and try to delete the file I had attached. However, when I try do it, I have a permission problem. When I kill the sending email program, I am able to delete the file.

I want to know how to configure the timeout to make sure it gives up sending the message, how I "detach" the file when the message is not sent and how to make the program wait for the message to be sent (I want to send the message and then erase the attached file from the computer. So I need to know when the message was really sent or when it was timedout).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-09-21 12:59:32

CDO 已经无可救药地过时了,您确实需要考虑切换到 System.Net.Mail。具体问题听起来像是文件锁定问题。就像 CDO 中的错误一样,它会打开附件来撰写电子邮件,但在 SMTP 服务器犹豫时忘记关闭文件。

.NET 处理 COM 服务器(如 CDO)的方式可能会加剧此错误。在垃圾收集器运行之前,COM 对象不会被释放。这可能需要一段时间,尤其是当您的程序在尝试发送电子邮件后没有执行任何重要操作时。解决方法是在 CDO 对象上调用 Marshal.ReleaseComObject()。当您的程序中有其他 CDO 接口引用时,往往不起作用,这些引用往往是隐藏的。在您清空任何对象引用之后,GC.Collect() + GC.WaitForPendingFinalizers() 是一把大锤子。

但实际上,请使用 System.Net.Mail。

CDO is hopelessly obsolete, you really need to consider switching to System.Net.Mail. The specific problem sounds like a file locking issue. Quacks like a bug in CDO, it would open the attachment to compose the email message but forgets to close the file when the SMTP server balks.

This bug is probably exacerbated by the way .NET deals with COM servers, like CDO. The COM object doesn't get released until the garbage collector runs. Which can take a while, especially when your program doesn't do anything significant after trying to send the email. A workaround for that is calling Marshal.ReleaseComObject() on the CDO object. Tends to not work when you have other CDO interface references in your program, those references tend to be hidden. GC.Collect() + GC.WaitForPendingFinalizers() is the big hammer, after you nulled any object reference.

But, really, use System.Net.Mail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文