The example for using EASendMail SMTP component asynchronously demonstrates using AsyncWaitHandle to block execution until the SendMail operation completes, like this:
SmtpClientAsyncResult oResult = oSmtp.BeginSendMail(oServer, oMail, null, null);
while (!oResult.IsCompleted) {
oResult.AsyncWaitHandle.WaitOne(50, false);
}
oSmtp.EndSendMail(oResult);
Is it possible to use callback delegate? I have tried to use the code like following:
SmtpClientAsyncResult oResult = oSmtp.BeginSendMail(oServer, oMail,
new AsyncCallback(AsyncSendCompletedCallback), oSomeStateObject);
//...
private void AsyncSendCompletedCallback(IAsyncResult result)
{
try
{
EASendMail.SmtpClientAsyncResult ar = result as EASendMail.SmtpClientAsyncResult;
ar.SmtpClientInstance.EndSendMail(ar);
}
catch( EASendMail.SmtpTerminatedException )
{
}
catch( Exception ex )
{
}
}
This works fine when communication with SMTP completes without problems. If there is a communication error, however, the operation will never complete, i.e. callback procedure seems to newer get called. For example, if wrong password is set, the opeation continues and various events are raised as expected, until the SMTP server responds with status code 535 Bad authentication. Then everything stops, and the callback procedure is never called. This does not happen when AsyncWaitHandle is used.
Can you confirm that callback delegate method is actually supported?