如何在通过 SmtpMailClient.SendAsync() 发送邮件之前阻止应用程序退出
我在通过 SmtpMailClient.SendAsync() 发送邮件时遇到问题,即如果应用程序在 SmtpMailClient.SendAsync() 之后立即关闭,则不会发送邮件。
那么如何强制应用程序在回调之前不关闭呢?
谢谢 !!
I have a issue with sending mail via SmtpMailClient.SendAsync(), i.e. if the application is closed immediately after SmtpMailClient.SendAsync(), mail is not sent.
So how to force the app not to close till the callback ?
Thanks !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SmtpClient 有一个 SendCompleted 事件。以下是有关如何实现它的示例代码: http://www.systemnetmail.com/faq/4.6 .aspx
根据您的平台,您需要实现某种 Application 对象,该对象对挂起的 SendAsync 操作进行计数,并且仅在它们为 0 时才允许应用程序结束。
The SmtpClient has a SendCompleted event. Here's a sample code on how to implement it: http://www.systemnetmail.com/faq/4.6.aspx
Depending on your platform, you need to implement some kind of an Application object which counts the pending SendAsync operations and only allow the application to end if they are 0.
添加 SendCompleted http://msdn.microsoft。 com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspx 事件处理程序到您的 SmtpClient 对象。
Add a SendCompleted http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspx event handler to your SmtpClient object.
我认为问题实际上是:如何拦截退出命令并根据应用程序状态推迟它?
对于 WinForms:
I think the question is really: How do you intercept a quit command and defer it based on application state?
For WinForms:
最后通过使用 bool 变量来查找邮件是否已发送。
代码:
现在在 FormClosing 事件中
希望它有帮助!
Finally did it by using a bool variable to find whether the mail is sent or not.
code:
Now in the FormClosing event
Hope it helps !!