我正在使用 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 的响应之前,如何阻止表单关闭?