Developer notes written down before they get lost.
Send Mails From OpenShift
OpenShift is a great place to host your applications. In this post I’m going to show you how you can easily send mails
from your JEE applications running on JBoss. For sending mails I’m going to configure an external SMTP server. Any SMTP
server will do, I’m going to use GMail.
In case you don’t already have an application ready, create one selecting the JBoss AS 7 cartridge. Clone the git
repository and open the JBoss standalone configuration: .openshift/config/standalone.xml. Look for the mail subsystem
which should look like this:
Enter your credentials for the SMTP server you want to use. In case you have activated application specific passwords
in your Google profile, the password is not your Google password, but an application specific one you have to create
first.
As you can see we’re referencing a named socket binding, which we’re going to create now. Towards the end of the
standalone configuration you can see all configured socket bindings:
We’re almost done! Now you can use the mail session in your application. In my case I’m using the mail session from an
Errai service, but any other server side class (REST endpoint, servlet, EJB, POJO) should
work also:
importstaticjavax.mail.Message.RecipientType.TO;importjava.util.Date;importjavax.annotation.Resource;importjavax.mail.Address;importjavax.mail.MessagingException;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importcom.google.common.base.Optional;importorg.jboss.errai.bus.client.api.messaging.Message;importorg.jboss.errai.bus.client.api.messaging.MessageCallback;importorg.jboss.errai.bus.server.annotations.Service;importorg.jboss.errai.common.client.protocols.MessageParts;@ServicepublicclassFeedbackServiceimplementsMessageCallback{@Resource(mappedName="java:/mail/Gmail")privateSessionmailSession;@Overridepublicvoidcallback(Messagemessage){// get data out of the message and persist feedback Stringguest=Optional.fromNullable(message.get(String.class,"guest")).or("n/a");booleancommitment=Optional.fromNullable(message.get(Boolean.class,"commitment")).or(false);saveFeedback(guest,commitment);// send response using the Errai message bus// see http://docs.jboss.org/errai/2.4.0.Beta1/errai/reference/html_single/#sid-5931263 // for more detailscreateConversation(message).subjectProvided().done().reply();// sending the mail might take a moment, so this is done *after* sending the response to the client.sendMail(guest,commitment);}privatevoidsaveFeedback(finalStringguest,finalbooleancommitment){...}privatevoidsendMail(finalStringguest,finalbooleancommitment){try{MimeMessagemessage=newMimeMessage(mailSession);Address[]to=newInternetAddress[]{newInternetAddress("your.name@gmail.com")};message.setRecipients(TO,to);message.setSubject("Your subject");message.setContent("Your message","text/plain");Transport.send(message);}catch(MessagingExceptione){// error handling}}}
That’s all - any feedback, thoughts and objections are welcome.