no

Learn to configure mail resource in Wildfly with a sample java client

This tutorial assumes that you have the following installed on your local system: JBoss 7.2 eclipse to run a project a mail server, can...

This tutorial assumes that you have the following installed on your local system:

  1. JBoss 7.2
  2. eclipse to run a project
  3. a mail server, can be gmail
In this page we will summarize how to configure a mail dataSource in Jboss and create a JavaEE6 client to send a sample email.

Configure Mail datasource in JBoss 7.2

  1. Open standalone.xml
  2. Search for the mail subsystem
    <subsystem xmlns="urn:jboss:domain:mail:1.1"></subsystem>
  3. Define a mail session
    <mail-session jndi-name="java:/czetsuyaMail">
     <smtp-server outbound-socket-binding-ref="mail-smtp">
      <login name="username@xxx.com" password="secret"/>
     </smtp-server>
    </mail-session>
    
  4. Define an outbound socket we will create 2: smtp with port 587 (eg zimbra) and ssl with port 465 (for gmail). Search for socket-binding-group section and add the following lines: for Zimbra
    <outbound-socket-binding name="mail-smtp">
     <remote-destination host="yourServer.net" port="587"/>
    </outbound-socket-binding>
    
    OR for gmail
    <outbound-socket-binding name="mail-smtp-gmail">
     <remote-destination host="smtp.gmail.com" port="465"/>
    </outbound-socket-binding>
    In greater version of JBoss now Wildfly, username and password must now be specified in the smtp-server tag:
    <smtp-server outbound-socket-binding-ref="mail-smtp-gmail" username="youremail@gmail.com" password="secret">
                    </smtp-server>
    

Sample Client
package org.czetsuya.test;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

@Startup
@Singleton
public class MailService {
 @Resource(lookup = "java:/czetsuyaMail")
 private Session mailSession;

 @PostConstruct
 private void init() throws AddressException, MessagingException {
  sendAsyncMessage("Test");
 }

 @Asynchronous
 private void sendAsyncMessage(String htmlMessage) throws AddressException,
   MessagingException {

  MimeMessage msg = new MimeMessage(mailSession);
  msg.setFrom(new InternetAddress("spamMeNot@gmail.com"));
  msg.setSubject("Test");
  msg.setSentDate(new Date());
  msg.setContent(htmlMessage, "text/html");

  msg.setRecipient(RecipientType.TO, new InternetAddress(
    "spamMeNot@gmail.com"));
  InternetAddress[] replytoAddress = { new InternetAddress(
    "spamMeNot@gmail.com") };
  msg.setReplyTo(replytoAddress);

  Transport.send(msg);
 }
}

You can actually test with telnet if your port is open by executing this command in command prompt:
telnet server port

Reference:
https://community.jboss.org/wiki/JBossAS720EmailSessionConfigurtion-EnglishVersion
http://commons.apache.org/proper/commons-email/userguide.html

Related

wildfly 1422908331514827490

Post a Comment Default Comments

item