tsereg
  • tsereg
  • 50.25% (Neutral)
  • Newbie Topic Starter
12 years ago
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?
ivan
  • ivan
  • 100% (Exalted)
  • Administration
12 years ago

Thank you very much for reporting this problem, after I double-checked the code, it is a bug in the callback. In current version, the callback delegate is not invoked if there is an exception.

I will send you a quick fix at tomorrow.

Thanks again.
tsereg
  • tsereg
  • 50.25% (Neutral)
  • Newbie Topic Starter
12 years ago
Thanks for the very quick reply.

I have temporarily cirumvented the problem by using BackgroundWorker as follows:
bwWaitAsync = new BackgroundWorker();
bwWaitAsync.DoWork += bwWaitAsync_DoWork;
bwWaitAsync.RunWorkerCompleted += bwWaitAsync_RunWorkerCompleted;

//...

bwWaitAsync.RunWorkerAsync(oSmtp.BeginSendMail(oServer, oMail, null, oSomeStateObject));

//...

void bwWaitAsync_DoWork(object sender, DoWorkEventArgs e)
{
EASendMail.SmtpClientAsyncResult result = e.Argument as EASendMail.SmtpClientAsyncResult;
result.AsyncWaitHandle.WaitOne();
e.Result = result;
}

void bwWaitAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
AsyncSendCompletedCallback(e.Result as EASendMail.SmtpClientAsyncResult);
}


This is not the optimal solution as it creates one more thread, but works fine.

So, there is no rush to fix the bug, I can wait until the next regular release.

Thank you again!
ivan
  • ivan
  • 100% (Exalted)
  • Administration
12 years ago

Hi, I have sent you a quick fix by private message, you can download and test it.
If it is ok, please kindly let me know, then i can add this fix at next release.

Thank you very much
tsereg
  • tsereg
  • 50.25% (Neutral)
  • Newbie Topic Starter
12 years ago
In my development environment, using first a wrong password, and then an unexisting host, EASendMail40.dll worked as expected, errors being reported as exceptions from EndSendMail.

I will be deploying the program at one site tomorrow, and will have it run experimentally during the next week. Will report if any issues arrise.

Thank you, and greetings!

ivan
  • ivan
  • 100% (Exalted)
  • Administration
12 years ago

Just let you know, we just released 7.0.0.2, this version includes the fix for callback.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.