You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide a way to handle success/failure in async mode (including testConnection()) #148
Hi!
It would be really useful if you could somehow handle success and failures when sending asynchronously. Is it be possible to implement CompletableFuture as a return value for mailer.sendAsync(...) or something?
I am interested to prepare a pull request.
Hmm, Simple Java Mail uses JDK 1.7, so it would need to be something else. Any preference?
Also, this issue will be solved another way with #121 when you can add custom steps in between the various stages of emailing (including handling exceptions). However, that won't be in the near future.
You can also implement your own (threaded) checking routine that checks f.isDone() on mutliple emails in your batch so your thread doesn't block for individual async emails being sent (otherwise what's the point of sending asynchronously).
Hi, thanks for the reply!
I wanted to avoid Future as a solution, because there is no way to register a callback with it. You need to block or implement some polling mechanism, as you described.
which the user can implement and pass into mailer.sendAsync(callback)?
This way everyone can write their own wrappers for integration with JDK8/Guava/Kotlin/etc...
changed the title [-]Provide a way to handle success/failure in async mode[/-][+]Provide a way to handle success/failure in async mode (including testConnection())[/+]on Jul 17, 2018
Future<?> f = asyncResponse.getFuture();
// f.get() actually blocks until done, below is an example custom loop// for checking result in a non-blocking way:while (!f.isDone()) Thread.sleep(100);
// result is in, check ittry {
f.get(); // without the above loop, this would actually block until doneSystem.err.println("success");
} catch (ExecutionExceptione) {
System.err.println("error");
}
AsyncResponse, its handlers, and .getFuture() were replaced by CompletableFuture in 7.0. Since 9.2, the returned future covers failures from the complete asynchronous operation; clear API mistakes such as a null email can still throw immediately. See the 7.0 migration, 9.2 failure-reporting change, and current async-result documentation.
Activity
bbottema commentedon Jun 30, 2018
Hmm, Simple Java Mail uses JDK 1.7, so it would need to be something else. Any preference?
Also, this issue will be solved another way with #121 when you can add custom steps in between the various stages of emailing (including handling exceptions). However, that won't be in the near future.
#148: Added support for handling async results including exception
bbottema commentedon Jul 1, 2018
I've added
Futureto the API for release 6.0.0. To use it:You can also implement your own (threaded) checking routine that checks
f.isDone()on mutliple emails in your batch so your thread doesn't block for individual async emails being sent (otherwise what's the point of sending asynchronously).Would this suit your needs?
trathschlag commentedon Jul 2, 2018
Hi, thanks for the reply!
I wanted to avoid
Futureas a solution, because there is no way to register a callback with it. You need to block or implement some polling mechanism, as you described.Would it be possible to provide a basic interface
which the user can implement and pass into
mailer.sendAsync(callback)?This way everyone can write their own wrappers for integration with JDK8/Guava/Kotlin/etc...
[-]Provide a way to handle success/failure in async mode[/-][+]Provide a way to handle success/failure in async mode (including testConnection())[/+]#148: implemented a more advanced async result handler approach in ad…
#148: doc typo
bbottema commentedon Jul 21, 2018
You got your wish @trathschlag!
Both sending emails and testing server connections can now be done asynchronously with either Future or Handlers.
Using Handlers
Using Future
bbottema commentedon Dec 18, 2019
Released in 6.0.0-rc1!!
bbottema commentedon Jan 24, 2020
6.0.0 has released as well, finally.
bbottema commentedon Jan 2, 2022
7.0.0 just released, which is a switch to Java8 and I've replaced this system with
CompletableFuturefor #367.bbottema commentedon Aug 26, 2026
Current 9.x usage (verified against 9.3.2):
AsyncResponse, its handlers, and.getFuture()were replaced byCompletableFuturein 7.0. Since 9.2, the returned future covers failures from the complete asynchronous operation; clear API mistakes such as a null email can still throw immediately. See the 7.0 migration, 9.2 failure-reporting change, and current async-result documentation.