I am running into an issue when trying to send an Email Async, I found out a no of post on Stackoverflow but none of them was helpful. I have following block of code
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage
("[email protected]", message.Destination, message.Subject, message.Body);
mailMessage.IsBodyHtml = true;
var client = new SmtpClient();
client.SendCompleted += (s, e) => client.Dispose();
client.SendAsync(mailMessage,null);
return Task.FromResult(0);
}
}
I got an email but getting an exception when this block of code run
an asynchronous module or handler completed while an asynchronous operation was still pending.
Any suggestion?
Use SendMailAsync
instead of SendAsync
:
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage
("[email protected]", message.Destination, message.Subject, message.Body);
mailMessage.IsBodyHtml = true;
using(var client = new SmtpClient())
{
await client.SendMailAsync(mailMessage);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With