JBoss AS 7.1.0 Mail

Since version 7.1.0 JBoss AS includes a mail subsystem by default and it seems to work with a local postfix installation out of the box. Here is a simple mailer bean:

import javax.annotation.security.RolesAllowed;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.jboss.logging.Logger;

/**
 *
 * @author manuel
 *
 */
@Model
public class TestMailController {

	@Inject
	private Session session;

	@Inject
	private Logger log;

	public void sendMail() {
		log.debug("sending mail");
		final Message msg = new MimeMessage(session);
		try {

			final InternetAddress addressFrom = new InternetAddress(
					"manuel@coffeebeans.at");
			msg.setFrom(addressFrom);
			log.debug("set from");

			final InternetAddress[] recipients = new InternetAddress[] {
                             new InternetAddress("manuel@localhost") };
			msg.setRecipients(Message.RecipientType.TO, recipients);
			log.debug("set recipients");

			msg.addHeader("CustomHeader", "some value");
			log.debug("added custom header");

			msg.setSubject("testmail");
			log.debug("set subject");

			msg.setContent("my message", "text/plain");
			log.debug("set message");

			Transport.send(msg);
			log.debug("message sent");
		} catch (final MessagingException exc) {
			log.error(exc);
		}
	}
}

Leave a Reply