![](/img/trans.png)
[英]How to determine the time it took SmtpClient.send to send the message?
[英]How can I cancel an SmtpClient.Send operation?
这是发送电子邮件即返回布尔值的方法
public bool SendEmail()
{
bool isSend = true;
try
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("kips321786@gmail.com","umerzaman100");
smtp.EnableSsl = true;
MailAddress to = new MailAddress("m.umer.zaman@gmail.com");
MailAddress from = new MailAddress("kips321786@gmail.com");
MailMessage mail = new MailMessage(from, to);
check = to.ToString();
mail.Subject = "This is a Test Mail ";
mail.Body = "WoW Mail is Receive";
lblStatus.Text=" Sending email...";
smtp.Send(mail);
Thread.Sleep(10000);
}
catch (Exception e)
{
check = e.Message.ToString();
isSend = false;
}
return isSend;
}
这是我的Mial Proccess方法,即异步处理,我有取消按钮,无法发送电子邮件,我应该在取消按钮方法中
public async void MailProccess(CancellationToken cancelToken =new CancellationToken())
{
Task<bool> task2 = new Task<bool>(SendEmail);
task2.Start();
bool msg = await task2;
if (msg)
{
lblStatus.Text = "Your Email Send....";
}
else
{
lblStatus.Text = "Your Email not Send....";
}
}
这是我的取消按钮方法
private void btnCancelEmail_Click(object sender, EventArgs e)
{
cts.Cancel();
lblStatus.Text = "Email Sending Cancel";
}
try{
smtp.Send(mail);
isSend = true;
}
catch(Exception ex)
{
isSend = false
}
请尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.