Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class MailSender

  1. Object
  2. MailSender

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

Constructors

public MailSender()

Methods

public void send(Mail mail) throws IOExceptionTakes mail for delivery and returns without waiting for it.
public void send(Mail... mails) throws IOExceptionTakes several mails; they leave over one connection where the transport has connections.
public void sendNow(Mail mail) throws IOExceptionSends mail on the calling thread and returns when the mail server has taken it, or throws a MailException saying why it did not.
public List<Mail> sent() throws IOExceptionThe mails sent while the server has no transport, oldest first, each with its template rendered and its sender filled in.
public void clearSent() throws IOExceptionForgets what sent answers; a test calls it before it acts.
public static MailSender current()The sender of the calling thread’s server, for code no bean is injected into.

Inherited methods

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.

send

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

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.

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.

clearSent

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

current

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