// 异步方法 private async Task SendEmailAsync() { using (var client = new SmtpClient()) { client.Host = "smtp.gmail.com"; client.Port = 587; client.EnableSsl = true; client.Credentials = new NetworkCredential("username", "password");
var message = new MailMessage("from@example.com", "to@example.com");
message.Subject = "Test Email";
message.Body = "This is a test email.";
try
{
await client.SendMailAsync(message);
}
catch (Exception ex)
{
// 处理异常
}
}
}
// 调用异步方法 var sendTask = SendEmailAsync(); if (await Task.WhenAny(sendTask, Task.Delay(10000)) == sendTask) { // 发送成功 } else { // 发送超时 }
上一篇:Android合并清单文件失败
下一篇:Android和CherryPy