Custom JavaEE 8 jca resource adapter for connecting to mail servers
TomEE 8
mvn clean compile install ; (cd ear-module/ ; mvn tomee:run)
Liberty/OpenLiberty
mvn clean compile install ; (cd ear-module/ ; mvn liberty:run)
Example Inbound Implementation :
Note: Inbound currently use imaps protocol , target mail server must supported idle connection.
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "name", propertyValue = "Google Gmail"),
@ActivationConfigProperty(propertyName = "hostname", propertyValue = "imap.gmail.com"),
@ActivationConfigProperty(propertyName = "port", propertyValue = "993"),
@ActivationConfigProperty(propertyName = "username", propertyValue = "USERNAME"),
@ActivationConfigProperty(propertyName = "password", propertyValue = "PASSWORD")
}
)
public class GoogleGmailMDB implements MailListener {
private static final Logger logger = Logger.getLogger(GoogleGmailMDB.class.getName());
@Override
public void receiveEmail(Email email) {
logger.info("[Google Gmail] Receive message subject: [" + email.getSubject() + "]");
}
}Example Outbound Implementation :
liberty/openLiberty configuration (server.xml) :
<connectionFactory jndiName="eis/outlookSmtpConnection">
<properties.jca-application.jca-resourceAdapter
debug="true"
hostname="smtp-mail.outlook.com"
portNumber="587"
username="USERNAME"
password="PASSWORD"/>
</connectionFactory>TomEE 8 configuration (server.xml) :
<Resource id="eis/outlookSmtpConnection" type="ir.moke.jca.api.SmtpConnectionFactory"
class-name="ir.moke.jca.adapter.SmtpManagedConnectionFactory">
ResourceAdapter=EmailResourceAdapter
TransactionSupport=none
debug=true
hostname=smtp-mail.outlook.com
portNumber=587
username=USERNAME
password=PASSWORD
</Resource>now example connectionFactory usage:
@Path("email")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SendEmailApi {
@Resource(name = "eis/outlookSmtpConnection")
private SmtpConnectionFactory scf;
@Path("send")
@POST
public void sendEmail(Email email) {
try {
SmtpConnection smtpConnection = scf.getConnection();
smtpConnection.sendEmail(email);
} catch (ResourceException e) {
e.printStackTrace();
}
}
}send email:
curl -X POST -H "Content-Type: application/json" http://localhost:8080/api/v1/email/send -d '{"subject":"Hello dear !","recipientList" : ["user@domain.com"],"content":"How are you ?","contentType":"text/plain"}'