- Turn on standalone WildFly instance
./<wild_fly_home>/bin/standalone.sh
- Add new Mail Session (assign JNDI name, you would like to use in your applications)
- When the session is created, click View and add some necessary properties like your username, password, SSL encryption etc
- Save changes and shut down Wildfly server (CTRL+C in terminal)
- Open standalone configuration file (<wild_fly_home>/standalone/configuration/standalone.xml) and replace lines:
<outbound-socket-binding name="mail-smtp">
<remote-destination host="localhost" port="25"/>
</outbound-socket-binding>
with
<outbound-socket-binding name="mail-smtp">
<remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>
- Start Wildfly (look 1.)
How to send mail from Java EE?
Use this EJB as an example:
import javax.annotation.Resource; import javax.ejb.Stateless; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.logging.Level; import java.util.logging.Logger; @Stateless public class Mail { @Resource(name = "java:jboss/mail/gmail") private Session session; public void send(String addresses, String topic, String textMessage) { try { Message message = new MimeMessage(session); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses)); message.setSubject(topic); message.setText(textMessage); Transport.send(message); } catch (MessagingException e) { Logger.getLogger(Mail.class.getName()).log(Level.WARNING, "Cannot send mail", e); } } }
Keep it simple!