From 4f027d1664cdc2127e893d12f1e8d1501b982b76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 8 Jul 2017 00:21:31 +0200 Subject: [PATCH] Please cherry-pick: - @Resource is not working in any unmanaged classes (like ordinary classes are) - so let's try it with the annotation-free JNDI lookup over InitialContext - the constructor may throw now a NamingException if the JNDI lookup failed - please note, the short name is now generally "smtpSession" MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../model/delivery/AddressbookMailer.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java b/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java index e0e9a5b..f308689 100644 --- a/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java +++ b/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java @@ -18,9 +18,11 @@ package org.mxchange.addressbook.mailer.model.delivery; import java.text.MessageFormat; import java.util.Map; -import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.mxchange.jmailee.model.delivery.BaseMailer; @@ -41,8 +43,20 @@ public class AddressbookMailer extends BaseMailer implements DeliverableAddressb /** * Email session */ - @Resource (name = "jmail/addressbook", description = "A JavaMail resource") - private Session mailSession; + private final Session mailSession; + + /** + * Default constructor + *

+ * @throws javax.naming.NamingException If the JNDI name is not found + */ + public AddressbookMailer () throws NamingException { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.mailSession = (Session) context.lookup("java:global/jfinancials-mailer/smtpSession"); //NOI18N + } @Override public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException { @@ -53,9 +67,9 @@ public class AddressbookMailer extends BaseMailer implements DeliverableAddressb if (null == emailWrapper) { // Throw NPE throw new NullPointerException("emailWrapper is null"); //NOI18N - } else if (emailWrapper.getRecipient() == null) { + } else if (emailWrapper.getRecipientAddress() == null) { // Throw NPE again - throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N + throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N } else if (emailWrapper.getSubjectLine() == null) { // ... and again throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N -- 2.39.5