How to configure Mail resource in Jboss 7.2 with a sample java client
Tech-Today

How to configure Mail resource in Jboss 7.2 with a sample java client


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="[email protected]" 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="[email protected]" 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("[email protected]"));
msg.setSubject("Test");
msg.setSentDate(new Date());
msg.setContent(htmlMessage, "text/html");

msg.setRecipient(RecipientType.TO, new InternetAddress(
"[email protected]"));
InternetAddress[] replytoAddress = { new InternetAddress(
"[email protected]") };
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




- How To Create A Wildfly Cluster
The following steps will help us configure a Wildfly cluster. *Note that I'm using Wildfly 11 *It took me some time to figure it out so I'm writing it here for reference. I'm using the standard-full-ha.xml profileIn the program arguments add  -b...

-
Send Email using MFMailComposer (Template)1.) Import MessageUI      import MessageUI 2.) Show Email func showMail() { let mailComposeViewController = configuredMailComposeViewController() if MFMailComposeViewController.canSendMail()...

- How To Send And Receive Stomp Message In Jboss 7.2
The following code is an example of how we can send and receive a stomp message with JBoss 7.2. package org.meveo.util; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Properties; import javax.naming.InitialContext;...

- How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from  oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....

- Send Email Using Codeigniter In 1and1 Server
I get a timeout at first try. Using the default mail type from codeigniter will not, to use the built in email feature from codeigniter we have to change protocol into smtp: Like this: $config['mailtype'] = 'html'; $config['protocol']...



Tech-Today








.