Less than a minute
With JBoss you can easily configure your mail profiles so that you can consume theme quickly when needed inside your java code.
Configuring the connection in config file enables you to change your connection, email providers, usernames, passwords… without the need of changing your code and redeploying.
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.
Configuring standalone.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<subsystem xmlns="urn:jboss:domain:mail:2.0"> <mail-session name="gmail-incoming" jndi-name="java:jboss/mail/gmail-incoming"> <imap-server outbound-socket-binding-ref="mail-gmail-incoming" ssl="true" username="example@gmail.com" password="password123"/> </mail-session> <mail-session name="gmail-outgoing" from="example@gmail.com" jndi-name="java:jboss/mail/gmail-outgoing"> <smtp-server ssl="true" outbound-socket-binding-ref="mail-gmail-outgoing" username="example@gmail.com" password="password123"/> </mail-session> </subsystem> <outbound-socket-binding name="mail-gmail-outgoing"> <remote-destination host="smtp.gmail.com" port="465"/> </outbound-socket-binding> <outbound-socket-binding name="mail-gmail-incoming" source-port="0" fixed-source-port="false"> <remote-destination host="imap.gmail.com" port="993"/> </outbound-socket-binding> |
Alternatively you can set up all of the configuration using Wildly management console. Go to Configuration->Subsystems->Mail
JavaClass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public class TestMail { @Resource(name = "java:jboss/mail/gmail-income") private Session sessionRecive; @Resource(name = "java:jboss/mail/gmail-outgoing") private Session sessionSend; public void getEmails() { Store store = sessionRecive.getStore(); store.connect(); Message[] messages = store.getDefaultFolder().getMessages(); for(Message message : messages) { System.out.println(message.getSubject()); } } public void sendEmail(String addresses, String topic, String textMessage) { Message message = new MimeMessage(sessionSend); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses)); message.setSubject(topic); message.setText(textMessage); Transport.send(message); } } |
Get full potential of mail handling by using Mail subsystem of JBoss and javax.mail
For full documentation on javax.mail reefer to this article – link .
Post A Reply