Class MailSender
java.lang.Object
com.codename1.backend.mail.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(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 -
Method Summary
Modifier and TypeMethodDescriptionvoidForgets whatsent()answers; a test calls it before it acts.static MailSendercurrent()The sender of the calling thread's server, for code no bean is injected into.voidTakesmailfor delivery and returns without waiting for it.voidTakes several mails; they leave over one connection where the transport has connections.voidSendsmailon the calling thread and returns when the mail server has taken it, or throws aMailExceptionsaying why it did not.sent()The mails sent while the server has no transport, oldest first, each with its template rendered and its sender filled in.
-
Constructor Details
-
MailSender
public MailSender()
-
-
Method Details
-
send
Takesmailfor delivery and returns without waiting for it.- Throws:
IOException
-
send
Takes several mails; they leave over one connection where the transport has connections.- Throws:
IOException
-
sendNow
Sendsmailon the calling thread and returns when the mail server has taken it, or throws aMailExceptionsaying why it did not. No queue, no retry, and no regard for a transaction.- Throws:
IOException
-
sent
The mails sent while the server has no transport, oldest first, each with its template rendered and its sender filled in.- Throws:
IOException
-
clearSent
Forgets whatsent()answers; a test calls it before it acts.- Throws:
IOException
-
current
The sender of the calling thread's server, for code no bean is injected into.
-