/*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016 Roland Häder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
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.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
/**
* An email class for sending out mails from templates
* <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
*/
public abstract class BaseMailer implements DeliverableEmail {
// Init template engine
this.templateEngine = new VelocityEngine();
+ this.templateEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); //NOI18N
+ this.templateEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); //NOI18N
this.templateEngine.init();
}
return this.templateEngine;
}
+ @Override
+ @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
+ public void init (final Properties properties) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("init: properties={0} - CALLED!", properties)); //NOI18N
+
+ // Are all required properties set?
+ if (null == properties) {
+ // Is null
+ throw new NullPointerException("properties is null"); //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
+ throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N
+ }
+
+ // Set it here
+ this.properties = properties;
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
+ }
+
/**
* Sends an email to given email address with subject line.
* <p>
* @throws MessagingException If something happened on message delivery
*/
private void sendMail (final Address emailAddress, final String subjectLine, final String body, final Session mailSession) throws MessagingException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMail: emailAddress={0},subjectLine={1},body={2},mailSession={3} - CALLED!", emailAddress, subjectLine, body, mailSession)); //NOI18N
+
+ // Are the additional properties set?
+ if (null == this.properties) {
+ // Nothing set
+ throw new NullPointerException("this.properties is null"); //NOI18N
+ }
+
// Get MIME message instance
MimeMessage message = new MimeMessage(mailSession);
message.setHeader("MIME-Version", "1.0"); //NOI18N
message.setHeader("Content-Type", "text/plain; charset=UTF-8"); //NOI18N
message.setHeader("Content-Transfer-Encoding", "8bit"); //NOI18N
- message.setHeader("Errors-To", this.properties.getProperty("mailer.errorsto")); //NOI18N
+
+ // Is property "errorsto" set ?
+ if ((this.properties.containsKey("mailer.errorsto")) && (!this.properties.getProperty("mailer.errorsto").isEmpty())) { //NOI18N
+ // Use this
+ message.setHeader("Errors-To", this.properties.getProperty("mailer.errorsto")); //NOI18N#
+ }
// Is the property "bouncesto" set?
- if (this.properties.containsKey("mailer.bouncesto")) { //NOI18N
+ if ((this.properties.containsKey("mailer.bouncesto")) && (!this.properties.getProperty("mailer.bouncesto").isEmpty())) { //NOI18N
// Use this
message.setHeader("Bounces-To", this.properties.getProperty("mailer.bouncesto")); //NOI18N
- } else {
- // Use "errorsto"
- message.setHeader("Bounces-To", this.properties.getProperty("mailer.errorsto")); //NOI18N
}
// Is the property "xloop" set?
- if (this.properties.containsKey("mailer.xloop")) { //NOI18N
+ if ((this.properties.containsKey("mailer.xloop")) && (!this.properties.getProperty("mailer.xloop").isEmpty())) { //NOI18N
// Use this
message.setHeader("X-Loop", this.properties.getProperty("mailer.xloop")); //NOI18N
- } else {
- // Use "errorsto"
- message.setHeader("X-Loop", this.properties.getProperty("mailer.errorsto")); //NOI18N
}
// Directly send email
} else if (emailWrapper.getTemplateName().isEmpty()) {
// Is empty
throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
- } else if (emailWrapper.getLocale()== null) {
+ } else if (emailWrapper.getLocale() == null) {
// Throw NPE again
throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
}