Class MailSender

java.lang.Object
com.codename1.backend.mail.MailSender

public final class MailSender extends Object

Sends mail through the server's transport: what an application injects, as it would Spring's JavaMailSender.

@Service
public class Signup {
    private final MailSender mail;
    public Signup(MailSender mail) { this.mail = mail; }

    @Transactional
    public void register(Account account) throws IOException {
        accounts.save(account);
        mail.send(Mail.to(account.getEmail())
                .subject("Welcome")
                .template("mail/welcome", new Model().addAttribute("name", account.getName()))
                .build());
    }
}

send(Mail) returns at once. The mail is encoded on the calling thread and handed to the server's mail thread, which delivers it, tries again when the mail server is unreachable and gives up when it refuses. Inside a database transaction the mail is written to an outbox table in that transaction and sent after the commit, so a rollback sends nothing and a crash between the commit and the send sends it when the server is back.

Where it goes is cn1.mail.url; see Mailers. Without one, mail is kept in memory and sent() answers it, which is how a test reads what its code sent.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Forgets what sent() answers; a test calls it before it acts.
    static MailSender
    The sender of the calling thread's server, for code no bean is injected into.
    void
    send(Mail mail)
    Takes mail for delivery and returns without waiting for it.
    void
    send(Mail... mails)
    Takes several mails; they leave over one connection where the transport has connections.
    void
    sendNow(Mail mail)
    Sends mail on the calling thread and returns when the mail server has taken it, or throws a MailException saying why it did not.
    The mails sent while the server has no transport, oldest first, each with its template rendered and its sender filled in.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MailSender

      public MailSender()
  • Method Details

    • send

      public void send(Mail mail) throws IOException
      Takes mail for delivery and returns without waiting for it.
      Throws:
      IOException
    • send

      public void send(Mail... mails) throws IOException
      Takes several mails; they leave over one connection where the transport has connections.
      Throws:
      IOException
    • sendNow

      public void sendNow(Mail mail) throws IOException
      Sends mail on the calling thread and returns when the mail server has taken it, or throws a MailException saying why it did not. No queue, no retry, and no regard for a transaction.
      Throws:
      IOException
    • sent

      public List<Mail> sent() throws IOException
      The mails sent while the server has no transport, oldest first, each with its template rendered and its sender filled in.
      Throws:
      IOException
    • clearSent

      public void clearSent() throws IOException
      Forgets what sent() answers; a test calls it before it acts.
      Throws:
      IOException
    • current

      public static MailSender current()
      The sender of the calling thread's server, for code no bean is injected into.