Tôi đã thực hiện một EmailService đơn giản cho Asp.Net nhận dạng 2.0 (thông qua giao diện IIdentityMessageService
Asp.Net Identity 2.0 - Cách triển khai IIdentityMessageService để thực hiện Async SMTP bằng SmtpClient? .
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// convert IdentityMessage to a MailMessage
var email =
new MailMessage(new MailAddress("[email protected]", "(do not reply)"),
new MailAddress(message.Destination))
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true
};
using (var client = new SmtpClient()) // SmtpClient configuration comes from config file
{
return client.SendMailAsync(email);
}
}
}
Để gửi email, tôi đi qua UserManager:
await _userManager.SendEmailAsync(user.Id, "Confirm your account","Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
Vấn đề là mà tôi nhận được một System.Threading.Tasks.TaskCanceledException
khi tôi gọi SendEmailAsync()
và nó không rõ tại sao.
Nếu tôi gửi email đồng bộ (client.Send(email)
), eve rything hoạt động tốt.
Vì vậy, câu hỏi của tôi là:
Làm thế nào để ngăn chặn sự TaskCanceledException khỏi bị ném? và (giả sử tôi có thể khắc phục được lỗi này),
Làm thế nào tôi nên đi về giao tiếp sai sót trong quá email gửi lại cho khách hàng (ví dụ, "không có người dùng như vậy ở đây" phản ứng loại từ SmtpClient?
Dưới đây là chi tiết của bạn trả lời https://stackoverflow.com/a/45789677/3835843 – Arif