I had the following for emailing, which worked:
private SmtpClient _client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("address@gmail.com", "password"),
EnableSsl = true
};
public void DoThis(){
_client.Send("from@gmail.com", to.Email, "Subject", "Body");}
public void DoThat(){
_client.Send("from@gmail.com", to.Email, "Subject", "Body");}
But it was blocking the web application until the email was sent, so I decided to try sending Asynchronously:
public void DoThis(){
var message = new MailMessage("from@gmail.com", to.Email, "Subject", "Body");
_client.SendAsync(message, null);
}
Which I can see getting treated asynchronously if I debug, but I always get the following:
An asynchronous module or handler completed while an asynchronous
operation was still pending.
What am I doing wrong?
Best Answer
I ended up reworking the way my Async email is sent with the following:
Note that this is a temporary solution, and that the correct way (it seems) to handle emailing is to use some kind of queue service paired with a worker role or other windows service to pop the messages.