在获得SmtpClient.SendCompleted Event的响应之前停止表单关闭

时间:2012-06-07 07:59:53

标签: c# .net

我正在使用SmtpClient发送电子邮件。我在Mail class中创建了一些函数:

private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    // Get the unique identifier for this asynchronous operation.
    String token = (string)e.UserState;

    if (e.Cancelled)
    {
        MailStatus = Status.CANCEL;
        MailStatusMessage = token + " Send canceled.";
    }
    else if (e.Error != null)
    {
        MailStatus = Status.ERROR;
        MailStatusMessage = token + " " + e.Error.ToString();
    }
    else
    {
        MailStatus = Status.SENT;
        MailStatusMessage = "Mail sent.";
    }

    mailSent = true;
}

public void SentEmail()
{
    client = new SmtpClient(Host, Port);
    client.Credentials = new NetworkCredential(UserName, Password);
    MailAddress from = new MailAddress(MerchantEmail, MerchantName);
    MailAddress to = new MailAddress(CustomerEmail);
    MailMessage message = new MailMessage(from, to);
    message.Body = EmailSubjectTemplate();
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.Subject = EmailSubjectTemplate();
    message.SubjectEncoding = System.Text.Encoding.UTF8;

    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

    client.SendAsync(message, "Sending message.");

    message.Dispose();
}

在我调用函数的表单中,在关闭表单之前,但在等待SendCompletedCallback的响应时,将执行this.Close():

Mail mail = new Mail();
mail.SentEmail();
this.Close();

如何在收到SendCompletedCallback的回复之前停止表单关闭?

2 个答案:

答案 0 :(得分:1)

如果您的用户决定强行关闭其计算机,则无法执行此操作。 (关闭,任务杀死或其他) 但是,您可以连接Form_Closing事件并将e.Cancel内的CloseEventArgs属性更改为true,也许使用消息框通知您的用户有待处理的操作。

首先添加到您的主窗体(或任何您称之为)的全局变量作为状态标志:

private bool eMailSentPendingComplete = false;

然后在你的SentMail方法中,在client.SentAsync:

之后添加此行
eMailSentPendingComplete = true;

SendCompletedCallback中将其重置为false 并在您的主表单中连接FormClosing事件:

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(eMailSentPendingComplete == true)
        {
             DialogResult dr = MessageBox.Show("Pending email, do you wish to close?", MEssageBoxButtons.YesNo);
             e.Cancel = (dr == DialogResult.Yes ? true : false);
        }
    }

同样在FormClosing事件中,您可以查看属性e.CloseReason以进行进一步优化。

答案 1 :(得分:0)

选项1

public class Mail 
{
    public delegate void MailSendComplete();
    public event MailSendComplete OnMailSendComplete;
    private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        // your code 
        // finally call the complete event
        OnMailSendComplete();
    }

    public void SentEmail()
    {
        // your code 
    }
}

通过调用表单订阅此事件:

Mail m = new Mail();
m.OnMailSendComplete += new Mail.MailSendComplete(m_OnMailSendComplete);
m.SentEmail();

收到完整的活动后,您可以关闭表单     void m_OnMailSendComplete()     {         this.Close();     }

选项2

创建Mail对象时,可以将当前表单引用传递给它

Mail mail = new Mail(this);

然后在SendCompletedCallback结束时,您可以关闭表单

public class Mail 
{
    public Form form { get; set; }
    public Mail(Form  f)
    {
        form = f;
    }

    private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        // your code 
        // finally close the form
        form.Close();
    }
}