{"id":134,"date":"2017-05-19T15:19:02","date_gmt":"2017-05-19T15:19:02","guid":{"rendered":"http:\/\/lazarbulic.com\/blog\/?p=134"},"modified":"2019-12-05T12:00:12","modified_gmt":"2019-12-05T12:00:12","slug":"jboss-wildfly-mail","status":"publish","type":"post","link":"https:\/\/lazarbulic.com\/blog\/2017\/05\/19\/jboss-wildfly-mail\/","title":{"rendered":"JBoss WildFly Mail"},"content":{"rendered":"<p>1 minute<\/p><p>With JBoss you can easily configure your mail profiles so that you can consume theme quickly when needed inside your java code.<!--more--><\/p>\n<p>Configuring the connection in config file enables you to change your connection, email providers, usernames, passwords&#8230; without the need of changing\u00a0your code and redeploying.<\/p>\n<p>In this example I will go over configuring your JBoss and showing examples on how to receive and send email. The test example I used is tested and working on WildFly 10.1 with Gmail mail server.<\/p>\n<p>Configuring <strong>standalone.sh<\/strong>:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;subsystem xmlns=\"urn:jboss:domain:mail:2.0\"&gt;\r\n    &lt;mail-session name=\"gmail-incoming\" jndi-name=\"java:jboss\/mail\/gmail-incoming\"&gt;\r\n        &lt;imap-server outbound-socket-binding-ref=\"mail-gmail-incoming\" ssl=\"true\" username=\"example@gmail.com\" password=\"password123\"\/&gt;\r\n    &lt;\/mail-session&gt;\r\n\r\n    &lt;mail-session name=\"gmail-outgoing\" from=\"example@gmail.com\" jndi-name=\"java:jboss\/mail\/gmail-outgoing\"&gt;\r\n        &lt;smtp-server ssl=\"true\" outbound-socket-binding-ref=\"mail-gmail-outgoing\" username=\"example@gmail.com\" password=\"password123\"\/&gt;\r\n    &lt;\/mail-session&gt;\r\n&lt;\/subsystem&gt;\r\n\r\n\r\n    &lt;outbound-socket-binding name=\"mail-gmail-outgoing\"&gt;\r\n        &lt;remote-destination host=\"smtp.gmail.com\" port=\"465\"\/&gt;\r\n    &lt;\/outbound-socket-binding&gt;\r\n    &lt;outbound-socket-binding name=\"mail-gmail-incoming\" source-port=\"0\" fixed-source-port=\"false\"&gt;\r\n        &lt;remote-destination host=\"imap.gmail.com\" port=\"993\"\/&gt;\r\n    &lt;\/outbound-socket-binding&gt;<\/pre>\n<p>Alternatively you can set up all of the configuration using Wildly management console. Go to Configuration-&gt;Subsystems-&gt;Mail<\/p>\n<p><strong>JavaClass:<\/strong><\/p>\n<pre class=\"lang:java decode:true \">public class TestMail {\r\n\r\n\t@Resource(name = \"java:jboss\/mail\/gmail-income\")\r\n    private Session sessionRecive;\r\n\r\n    @Resource(name = \"java:jboss\/mail\/gmail-outgoing\")\r\n    private Session sessionSend;\r\n\r\n\r\n    public void getEmails() {\r\n    \tStore store = sessionRecive.getStore();\r\n    \tstore.connect();\r\n\r\n    \tMessage[] messages = store.getDefaultFolder().getMessages();\r\n\r\n\r\n    \tfor(Message message : messages) {\r\n    \t\tSystem.out.println(message.getSubject());\r\n    \t}\r\n    }\r\n\r\n    public void sendEmail(String addresses, String topic, String textMessage) {\r\n            Message message = new MimeMessage(sessionSend);\r\n            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses));\r\n            message.setSubject(topic);\r\n            message.setText(textMessage);\r\n\r\n            Transport.send(message);\r\n\r\n    }\r\n}<\/pre>\n<p>Get full potential of mail handling by using Mail subsystem of JBoss and \u00a0javax.mail<\/p>\n<p>For full documentation on javax.mail reefer to this article &#8211; <a href=\"https:\/\/javaee.github.io\/javamail\/\" target=\"_blank\" rel=\"noopener noreferrer\">link<\/a> .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 minuteWith JBoss you can easily configure your mail profiles so that you can consume theme quickly when needed inside your java code.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[20,17,14,15,19,21,18,16],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":15,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lazarbulic.com\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}