import java.text.MessageFormat;
import java.util.Date;
import java.util.Properties;
+import javax.mail.Address;
+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 javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
-import org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+import org.mxchange.jcoreee.database.BaseDatabaseBean;
import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
/**
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
-public abstract class BaseMailerBean implements DeliverableEmailLocal {
+public abstract class BaseMailerBean extends BaseDatabaseBean implements DeliverableEmailLocal {
/**
* Serial number
*/
private static final long serialVersionUID = 14_598_912_753_106L;
- /**
- * Logger bean
- */
- @Log
- private LoggerBeanLocal loggerBeanLocal;
-
/**
* Properties for this mailer
* <p>
* Default constructor
*/
protected BaseMailerBean () {
- try {
- // Get initial context
- Context context = new InitialContext();
-
- // Lookup logger
- this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
- } catch (final NamingException ex) {
- // Continue to throw
- throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
- }
+ // Call super constructor
+ super();
// Init template engine
this.templateEngine = new VelocityEngine();
*/
private void deliverMail (final WrapableEmailDelivery emailWrapper, final StringWriter writer, final Session mailSession) throws MessagingException {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMail: emailWrapper={0},body={1},mailSession={2} - CALLED!", emailWrapper, writer, mailSession)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMail: emailWrapper={0},body={1},mailSession={2} - CALLED!", emailWrapper, writer, mailSession)); //NOI18N
// Are the additional properties and all parameter set?
if (null == this.properties) {
// Nothing set
throw new NullPointerException("this.properties is null"); //NOI18N
+ } else if (!this.properties.containsKey("mailer.from")) { //NOI18N
+ // Throw exception
+ throw new IllegalStateException("this.properties.mailer.from is not set"); //NOI18N
} else if (null == emailWrapper) {
// Throw NPE
throw new NullPointerException("emailWrapper is null"); //NOI18N
throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
} else if (emailWrapper.getRecipientAddress() == null) {
// Throw it again
- throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
+ throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
} else if (emailWrapper.getTemplateName() == null) {
// Throw it again
throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
throw new NullPointerException("mailSession is null"); //NOI18N
}
+ // Parse from address
+ Address senderAddress = new InternetAddress(this.properties.getProperty("mailer.from"));
+
// Get MIME message instance
MimeMessage message = new MimeMessage(mailSession);
// Set subject, recipients and body
+ message.setFrom(senderAddress);
message.setSubject(emailWrapper.getSubjectLine());
- message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailWrapper.getRecipientAddress().toString(), true));
+ message.setRecipient(Message.RecipientType.TO, emailWrapper.getRecipientAddress());
message.setSentDate(new Date());
message.setText(writer.toString());
message.setHeader("MIME-Version", "1.0"); //NOI18N
Transport.send(message);
// Trace message
- this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
+ this.getLoggerBeanLocal().logTrace("deliverMail: EXIT!"); //NOI18N
}
/**
*/
protected void deliverMailWithTemplate (final Template template, final VelocityContext context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException {
// Log trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMailTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMailWithTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
// The parameters must be valid
if (null == template) {
throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
} else if (emailWrapper.getRecipientAddress() == null) {
// Throw it again
- throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
+ throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
} else if (emailWrapper.getTemplateName() == null) {
// Throw it again
throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
this.deliverMail(emailWrapper, writer, mailSession);
// Trace message
- this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
- }
-
- /**
- * Getter for logger bean
- * <p>
- * @return Local logger bean
- */
- protected LoggerBeanLocal getLoggerBeanLocal () {
- return this.loggerBeanLocal;
+ this.getLoggerBeanLocal().logTrace("deliverMailWithTemplate: EXIT!"); //NOI18N
}
/**
if (null == properties) {
// Is null
throw new NullPointerException("properties is null"); //NOI18N
+ } else if (!properties.containsKey("mailer.from")) { //NOI18N
+ // From not set
+ throw new IllegalArgumentException("properties.mailer.from is not set"); //NOI18N
} else if (!properties.containsKey("mailer.errorsto")) { //NOI18N
// Errors-To not set
throw new IllegalArgumentException("properties.mailer.errorsto is not set"); //NOI18N
} else if (!properties.containsKey("mailer.bouncesto")) { //NOI18N
- // Errors-To not set
+ // Bounces-To not set
throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N
}