Sending Emails
Emails are sent using SendMailSession. Mail session encapsulates the process of preparing emails, opening and closing transport connections, and sending emails.
Mail session is created by the MailServer.
1
SmtpServer smtpServer = MailServer.create()
2
.host("http://mail.com")
3
.port(21)
4
.buildSmtpMailServer();
5
...
6
SendMailSession session = smtpServer.createSession();
7
session.open();
8
session.sendMail(email1);
9
session.sendMail(email2);
10
session.close();
Copied!
Since opening session and sending emails may produce EmailException, it is necessary to wrap methods in try-catch block and closing the session in the finally block.

Sending using SSL

The preferred way of sending e-mails is by using SSL protocol. Jodd supports secure e-mail sending. Just set the ssl() flag while creating the server.
Here is an example of sending e-mail via Gmail (port 465 is set by default):
1
SmtpServer smtpServer = MailServer.create()
2
.ssl(true)
3
.host("smtp.gmail.com")
4
.auth("[email protected]", "password")
5
.buildSmtpMailServer();
6
...
7
SendMailSession session = smtpServer.createSession();
8
session.open();
9
session.sendMail(email);
10
session.close();
Copied!
Everything is the same, just a different session provider is used.
Last modified 1yr ago
Copy link