I'm working on an MVC C# website, .NET 4.0, and in one controller I need to send a lot of mail. I copied my own code from a standalone program where I use SendAsynch passing a callback. In the stanalone application all works.
Of course, in the controller the same code does not work: the callback is never called.
Someone can tell me WHY the same code works standalone and not in a controller?
Here is the main code:
SmtpClient GenMailClient = new SmtpClient();
GenMailClient.SendCompleted += SendCompleted;
GenMailClient.SendAsync(message, ArgumentForTheCallBack);
WaitingMails++;
var startTime = DateTime.Now;
//waits until the SendCompleted is called (FOREVER!)
while (WaitingMails != 0)
Thread.Sleep(500);
GenMailClient.Dispose();
And here is the SendCompleted callback:
private void SendCompleted(object sender, AsyncCompletedEventArgs e)
{
WaitingMails--;
}