Sending emails in JAVA EE 6

I am developing a Java EE 6 application deployed on a glass shawl and continue to read tutorials on how to send emails, but they seem outdated or too complicated. I was hoping there could be a pretty simple way to send mail in this specification, as many things have become so much easier. Can you point me in the right direction or show me some sample code?

+7
source share
2 answers

You can use apache commons email or if you use Spring, then use spring mail . There is always JavaMail if you do not want to use any of the wrapper libraries and sample code on it.

All of these links contain sample code.

+12
source

The JEE application server must provide an email resource. The only thing you need to do is find the resource (I believe it is configured) and send an email.

//Mail Resource injection not working on wildfly 10 //@Resource(lookup = "java:/futuramail") private Session mailSession; @Asynchronous @Lock(LockType.READ) public void sendMail(String recipient, String subject, String text) { try { InitialContext ic = new InitialContext(); mailSession = (Session) ic.lookup("java:/futuramail"); MimeMessage message = new MimeMessage(mailSession); Address[] to = new InternetAddress[]{new InternetAddress(recipient)}; message.setRecipients(Message.RecipientType.TO, to); message.setSubject(subject); message.setSentDate(new Date()); message.setContent(text, "text/html"); //message.setText(text); Transport.send(message); System.out.println("mail sent"); } catch (MessagingException me) { me.printStackTrace(); } catch (NamingException ex) { Logger.getLogger(MailProcessor.class.getName()).log(Level.SEVERE, null, ex); } } 
0
source

All Articles