2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jmailee.model.delivery;
19 import java.io.StringWriter;
20 import java.text.MessageFormat;
21 import java.util.Date;
22 import java.util.Properties;
23 import javax.mail.Address;
24 import javax.mail.MessagingException;
25 import javax.mail.Session;
26 import javax.mail.Transport;
27 import javax.mail.internet.InternetAddress;
28 import javax.mail.internet.MimeMessage;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.VelocityEngine;
35 import org.apache.velocity.runtime.RuntimeConstants;
36 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
37 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
38 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
39 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
42 * An email class for sending out mails from templates
44 * @author Roland Haeder<roland@mxchange.org>
46 public abstract class BaseMailer implements DeliverableEmail {
51 private static final long serialVersionUID = 14_598_912_753_106L;
57 private LoggerBeanLocal loggerBeanLocal;
60 * Properties for this mailer
63 * - mailer.errorsto = Email address for "Errors-To" header
64 * - mailer.bouncesto = Email address for "Bounces-To" header (optional, if not set, errorsto must be set)
65 * - mailer.xloop = Email address for "X-Loop" header (optional, if not set, errorsto must be set)
67 private Properties properties;
72 private final VelocityEngine templateEngine;
77 protected BaseMailer () {
79 // Get initial context
80 Context context = new InitialContext();
83 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
84 } catch (final NamingException ex) {
86 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
89 // Init template engine
90 this.templateEngine = new VelocityEngine();
91 this.templateEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); //NOI18N
92 this.templateEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); //NOI18N
93 this.templateEngine.init();
97 public VelocityEngine getTemplateEngine () {
98 return this.templateEngine;
102 public void init (final Properties properties) {
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("init: properties={0} - CALLED!", properties)); //NOI18N
106 // Are all required properties set?
107 if (null == properties) {
109 throw new NullPointerException("properties is null"); //NOI18N
110 } else if (!properties.containsKey("mailer.errorsto")) { //NOI18N
112 throw new IllegalArgumentException("properties.mailer.errorsto is not set"); //NOI18N
113 } else if (!properties.containsKey("mailer.bouncesto")) { //NOI18N
115 throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N
119 this.properties = properties;
122 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
126 * Sends an email to given email address with subject line.
128 * @param emailAddress Email address for recipient
129 * @param subjectLine Subject line
130 * @param body Body part
131 * @param mailSession Corresponding mail session to use
133 * @throws MessagingException If something happened on message delivery
135 private void sendMail (final Address emailAddress, final String subjectLine, final String body, final Session mailSession) throws MessagingException {
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMail: emailAddress={0},subjectLine={1},body={2},mailSession={3} - CALLED!", emailAddress, subjectLine, body, mailSession)); //NOI18N
139 // Are the additional properties set?
140 if (null == this.properties) {
142 throw new NullPointerException("this.properties is null"); //NOI18N
145 // Get MIME message instance
146 MimeMessage message = new MimeMessage(mailSession);
148 // Set subject, recipients and body
149 message.setSubject(subjectLine);
150 message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress.toString(), true));
151 message.setSentDate(new Date());
152 message.setText(body);
153 message.setHeader("MIME-Version", "1.0"); //NOI18N
154 message.setHeader("Content-Type", "text/plain; charset=UTF-8"); //NOI18N
155 message.setHeader("Content-Transfer-Encoding", "8bit"); //NOI18N
156 message.setHeader("Errors-To", this.properties.getProperty("mailer.errorsto")); //NOI18N
158 // Is the property "bouncesto" set?
159 if (this.properties.containsKey("mailer.bouncesto")) { //NOI18N
161 message.setHeader("Bounces-To", this.properties.getProperty("mailer.bouncesto")); //NOI18N
164 message.setHeader("Bounces-To", this.properties.getProperty("mailer.errorsto")); //NOI18N
167 // Is the property "xloop" set?
168 if (this.properties.containsKey("mailer.xloop")) { //NOI18N
170 message.setHeader("X-Loop", this.properties.getProperty("mailer.xloop")); //NOI18N
173 message.setHeader("X-Loop", this.properties.getProperty("mailer.errorsto")); //NOI18N
176 // Directly send email
177 Transport.send(message);
181 * Getter for logger bean
183 * @return Local logger bean
185 protected LoggerBeanLocal getLoggerBeanLocal () {
186 return this.loggerBeanLocal;
190 * Sends given mail template to all addresses found in email wrapper
192 * @param template Template to send
193 * @param context Velocity context
194 * @param emailWrapper Email wrapper containing recipient and such
195 * @param mailSession Mail session
197 * @throws MessagingException If something happened on message delivery
199 protected void sendMailTemplate (final Template template, final VelocityContext context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException {
201 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMailTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
203 // The parameters must be valid
204 if (null == template) {
206 throw new NullPointerException("template is null"); //NOI18N
207 } else if (null == emailWrapper) {
209 throw new NullPointerException("emailWrapper is null"); //NOI18N
210 } else if (emailWrapper.getRecipient() == null) {
212 throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
213 } else if (emailWrapper.getSubjectLine() == null) {
215 throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
216 } else if (emailWrapper.getSubjectLine().isEmpty()) {
218 throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
219 } else if (emailWrapper.getTemplateName() == null) {
221 throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
222 } else if (emailWrapper.getTemplateName().isEmpty()) {
224 throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
225 } else if (emailWrapper.getLocale() == null) {
227 throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
230 // Get writer instance
231 StringWriter writer = new StringWriter();
234 template.merge(context, writer);
236 // Get all out and send it
237 this.sendMail(emailWrapper.getRecipient(), emailWrapper.getSubjectLine(), writer.toString(), mailSession);