Skip to content

Provide a way to handle success/failure in async mode (including testConnection()) #148

Closed

Description

@trathschlag

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.

Thanks in advance!

Activity

  1. bbottema commented on Jun 30, 2018

    @bbottema
    Owner

    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.

  2. added this to the 6.0.0 milestone on Jun 30, 2018
  3. added a commit that references this issue on Jul 1, 2018
  4. bbottema commented on Jul 1, 2018

    @bbottema
    Owner

    I've added Future to the API for release 6.0.0. To use it:

    try {
    	f.get(); // blocks
    } catch (ExecutionException e) {
    	System.err.println("Sending email failed");
    	e.printStackTrace(System.err);
    }
    

    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?

  5. self-assigned this
    on Jul 1, 2018
  6. trathschlag commented on Jul 2, 2018

    @trathschlag
    Author

    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.

    Would it be possible to provide a basic interface

    interface MailCallback {
        void onSuccess();
        void onFailure(Exception e);
    }

    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...

  7. 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
  8. added 2 commits that reference this issue on Jul 21, 2018
  9. bbottema commented on Jul 21, 2018

    @bbottema
    Owner

    You got your wish @trathschlag!

    Both sending emails and testing server connections can now be done asynchronously with either Future or Handlers.

    Using Handlers

    AsyncResponse asyncResponse = mailer.sendMail(email, true);
    // or: mailer.testConnection(email, true);
    
    asyncResponse.onSuccess(() -> System.out.println("Success"));
    asyncResponse.onException((e) -> System.err.println("error"));

    Using Future

    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 it
    try {
      f.get(); // without the above loop, this would actually block until done
      System.err.println("success");
    } catch (ExecutionException e) {
      System.err.println("error");
    }
  10. removed their assignment
    on Aug 1, 2018
  11. bbottema commented on Dec 18, 2019

    @bbottema
    Owner

    Released in 6.0.0-rc1!!

  12. bbottema commented on Jan 24, 2020

    @bbottema
    Owner

    6.0.0 has released as well, finally.

  13. bbottema commented on Jan 2, 2022

    @bbottema
    Owner

    7.0.0 just released, which is a switch to Java8 and I've replaced this system with CompletableFuture for #367.

  14. bbottema commented on Aug 26, 2026

    @bbottema
    Owner

    Current 9.x usage (verified against 9.3.2):

    mailer.sendMail(email, true)
        .whenComplete((unused, failure) -> {
            if (failure != null) {
                System.err.println(failure.getMessage());
            }
        });
    
    mailer.testConnection(true)
        .whenComplete((unused, failure) -> {
            if (failure != null) {
                System.err.println(failure.getMessage());
            }
        });

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions