/*
- * 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
*/
package org.mxchange.jmailee.model.delivery;
+import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Date;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
+import java.util.Properties;
+import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
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.jusercore.model.email_address.ChangeableEmailAddress;
+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 {
@Log
private LoggerBeanLocal loggerBeanLocal;
+ /**
+ * Properties for this mailer
+ * <p>
+ * Valid are: - mailer.errorsto = Email address for "Errors-To" header -
+ * mailer.bouncesto = Email address for "Bounces-To" header (optional, if
+ * not set, errorsto must be set) - mailer.xloop = Email address for
+ * "X-Loop" header (optional, if not set, errorsto must be set)
+ */
+ private Properties properties;
+
+ /**
+ * Template engine
+ */
+ private final VelocityEngine templateEngine;
+
/**
* Default constructor
*/
// Continue to throw
throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
}
+
+ // 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();
}
@Override
- public void sendEmailChangeMail (final MessageProducer messageProducer, final ObjectMessage message, final ChangeableEmailAddress emailChange) {
+ public VelocityEngine getTemplateEngine () {
+ return this.templateEngine;
+ }
+
+ @Override
+ @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
+ public void init (final Properties properties) {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmailChangeMail: messageProducer={0},message={1},emailChange={2} - CALLED", messageProducer, message, emailChange)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("init: properties={0} - CALLED!", properties)); //NOI18N
- // All parameters + some sub objects must be set
- if (null == messageProducer) {
- // Throw NPE
- throw new NullPointerException("messageProducer is null"); //NOI18N
- } else if (null == message) {
- // Throw NPE again
- throw new NullPointerException("message is null"); //NOI18N
- } else if (null == emailChange) {
- // ... and again ...
- throw new NullPointerException("emailChange is null"); //NOI18N
- } else if (emailChange.getEmailChangeUser() == null) {
- // Throw NPE again
- throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
- } else if (emailChange.getEmailChangeUser().getUserId() == null) {
- // Throw NPE again
- throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
- } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
- // Not valid id
- throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
- } else if (emailChange.getEmailChangeUser().getUserContact() == null) {
- // Throw NPE again
- throw new NullPointerException("emailChange.emailChangeUser.userContact is null"); //NOI18N
- } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("emailChange.emailChangeUser.userContact.contactId is null"); //NOI18N
- } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() < 1) {
- // Not valid id
- throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userContact.contactId={0} is invalid.", emailChange.getEmailChangeUser().getUserContact().getContactId())); //NOI18N
- } else if (emailChange.getEmailAddress().trim().isEmpty()) {
- // Email address is empty
- throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //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
}
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
+ // Set it here
+ this.properties = properties;
- /**
- * Getter for logger bean
- * <p>
- * @return Local logger bean
- */
- protected LoggerBeanLocal getLoggerBeanLocal () {
- return this.loggerBeanLocal;
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
}
/**
* Sends an email to given email address with subject line.
* <p>
* @param emailAddress Email address for recipient
- * @param subjectLine Subject line
- * @param body Body part
- * @param mailSession Corresponding mail session to use
+ * @param subjectLine Subject line
+ * @param body Body part
+ * @param mailSession Corresponding mail session to use
* <p>
- * @throws NamingException If the resource cannot be found
* @throws MessagingException If something happened on message delivery
*/
- protected void sendMail (final String emailAddress, final String subjectLine, final String body, final Session mailSession) throws NamingException, MessagingException {
+ 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);
// Set subject, recipients and body
message.setSubject(subjectLine);
- message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
+ message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress.toString(), true));
message.setSentDate(new Date());
message.setText(body);
+ message.setHeader("MIME-Version", "1.0"); //NOI18N
+ message.setHeader("Content-Type", "text/plain; charset=UTF-8"); //NOI18N
+ message.setHeader("Content-Transfer-Encoding", "8bit"); //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")) && (!this.properties.getProperty("mailer.bouncesto").isEmpty())) { //NOI18N
+ // Use this
+ message.setHeader("Bounces-To", this.properties.getProperty("mailer.bouncesto")); //NOI18N
+ }
+
+ // Is the property "xloop" set?
+ 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
+ }
// Directly send email
Transport.send(message);
}
+ /**
+ * Getter for logger bean
+ * <p>
+ * @return Local logger bean
+ */
+ protected LoggerBeanLocal getLoggerBeanLocal () {
+ return this.loggerBeanLocal;
+ }
+
+ /**
+ * Sends given mail template to all addresses found in email wrapper
+ * <p>
+ * @param template Template to send
+ * @param context Velocity context
+ * @param emailWrapper Email wrapper containing recipient and such
+ * @param mailSession Mail session
+ * <p>
+ * @throws MessagingException If something happened on message delivery
+ */
+ protected void sendMailTemplate (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
+
+ // The parameters must be valid
+ if (null == template) {
+ // Throw NPE
+ throw new NullPointerException("template is null"); //NOI18N
+ } else if (null == emailWrapper) {
+ // Throw NPE
+ throw new NullPointerException("emailWrapper is null"); //NOI18N
+ } else if (emailWrapper.getRecipient() == null) {
+ // Throw NPE again
+ throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
+ } else if (emailWrapper.getSubjectLine() == null) {
+ // ... and again
+ throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
+ } else if (emailWrapper.getSubjectLine().isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
+ } else if (emailWrapper.getTemplateName() == null) {
+ // ... and again
+ throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
+ } else if (emailWrapper.getTemplateName().isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
+ } else if (emailWrapper.getLocale() == null) {
+ // Throw NPE again
+ throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
+ }
+
+ // Get writer instance
+ StringWriter writer = new StringWriter();
+
+ // Merge template
+ template.merge(context, writer);
+
+ // Get all out and send it
+ this.sendMail(emailWrapper.getRecipient(), emailWrapper.getSubjectLine(), writer.toString(), mailSession);
+ }
+
}