如何在通过 SmtpMailClient.SendAsync() 发送邮件之前阻止应用程序退出

发布于 2024-12-01 05:11:25 字数 143 浏览 1 评论 0原文

我在通过 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 技术交流群。

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

发布评论

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

评论(4

So要识趣 2024-12-08 05:11:25

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.

温柔嚣张 2024-12-08 05:11:25

添加 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.

原谅过去的我 2024-12-08 05:11:25

我认为问题实际上是:如何拦截退出命令并根据应用程序状态推迟它?

对于 WinForms:

protected override void OnFormClosing(FormClosingEventArgs e) 
{ 
    if (isSending) 
    { 
        quitOnSent = true; // make it so the quit will eventually happen
        e.Cancel = true; // prevent the quit for now
    } 
    base.OnFormClosing(e); 
} 

void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
    if(quitOnSent) this.Close(); // now quit
}

I think the question is really: How do you intercept a quit command and defer it based on application state?

For WinForms:

protected override void OnFormClosing(FormClosingEventArgs e) 
{ 
    if (isSending) 
    { 
        quitOnSent = true; // make it so the quit will eventually happen
        e.Cancel = true; // prevent the quit for now
    } 
    base.OnFormClosing(e); 
} 

void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
    if(quitOnSent) this.Close(); // now quit
}
Hello爱情风 2024-12-08 05:11:25

最后通过使用 bool 变量来查找邮件是否已发送。

代码:

   public static bool mailsent ;

   // I am not posting the mail sending code as its available every where.

    private void sendMailComplete(Object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

        MailMessage msd = e.UserState as MailMessage;

        if (!e.Cancelled) 
        {
            MessageBox.Show("Cancelled");
        }
         if (e.Error != null)
        {
            MessageBox.Show("Error");

        }

        else
        {
            mailsent = true;
        }

    }

现在在 FormClosing 事件中

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e){

        if (!mailsent)
        {
            MessageBox.Show("Please wait, Mail Sending in Process !!! ");
            e.Cancel = true;
        }
    }

希望它有帮助!

Finally did it by using a bool variable to find whether the mail is sent or not.

code:

   public static bool mailsent ;

   // I am not posting the mail sending code as its available every where.

    private void sendMailComplete(Object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

        MailMessage msd = e.UserState as MailMessage;

        if (!e.Cancelled) 
        {
            MessageBox.Show("Cancelled");
        }
         if (e.Error != null)
        {
            MessageBox.Show("Error");

        }

        else
        {
            mailsent = true;
        }

    }

Now in the FormClosing event

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e){

        if (!mailsent)
        {
            MessageBox.Show("Please wait, Mail Sending in Process !!! ");
            e.Cancel = true;
        }
    }

Hope it helps !!

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