2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.Message;
25 import javax.mail.MessagingException;
26 import javax.mail.Session;
27 import javax.mail.Transport;
28 import javax.mail.internet.InternetAddress;
29 import javax.mail.internet.MimeMessage;
30 import org.apache.velocity.Template;
31 import org.apache.velocity.app.VelocityEngine;
32 import org.apache.velocity.context.Context;
33 import org.apache.velocity.runtime.RuntimeConstants;
34 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
35 import org.mxchange.jcoreee.bean.ejb.BaseEnterpriseBean;
36 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
39 * A general email bean for sending out mails from templates
41 * @author Roland Häder<roland@mxchange.org>
43 public abstract class BaseMailerBean extends BaseEnterpriseBean implements DeliverableEmailRemote {
48 private static final long serialVersionUID = 14_598_912_753_106L;
51 * Properties for this mailer
53 * Valid are: - mailer.errorsto = Email address for "Errors-To" header -
54 * mailer.bouncesto = Email address for "Bounces-To" header (optional, if
55 * not set, errorsto must be set) - mailer.xloop = Email address for
56 * "X-Loop" header (optional, if not set, errorsto must be set)
58 private Properties properties;
63 private final VelocityEngine templateEngine;
68 protected BaseMailerBean () {
69 // Call super constructor
72 // Init template engine
73 this.templateEngine = new VelocityEngine();
74 this.templateEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); //NOI18N
75 this.templateEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); //NOI18N
76 this.templateEngine.init();
80 * Sends an email to given email address with subject line.
82 * @param emailAddress Email address for recipient
83 * @param subjectLine Subject line
84 * @param writer Body part
85 * @param mailSession Corresponding mail session to use
87 * @throws MessagingException If something happened on message delivery
89 private void deliverMail (final WrapableEmailDelivery emailWrapper, final StringWriter writer, final Session mailSession) throws MessagingException {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMail: emailWrapper={0},body={1},mailSession={2} - CALLED!", emailWrapper, writer, mailSession)); //NOI18N
94 final Address senderAddress = new InternetAddress(this.properties.getProperty("mailer.from"));
96 // Get MIME message instance
97 final MimeMessage message = new MimeMessage(mailSession);
99 // Set subject, recipients and body
100 message.setFrom(senderAddress);
101 message.setSubject(emailWrapper.getSubjectLine());
102 message.setRecipient(Message.RecipientType.TO, emailWrapper.getRecipientAddress());
103 message.setSentDate(new Date());
104 message.setText(writer.toString());
105 message.setHeader("MIME-Version", "1.0"); //NOI18N
106 message.setHeader("Content-Type", "text/plain; charset=UTF-8"); //NOI18N
107 message.setHeader("Content-Transfer-Encoding", "8bit"); //NOI18N
109 // Is property "errorsto" set ?
110 if ((this.properties.containsKey("mailer.errorsto")) && (!this.properties.getProperty("mailer.errorsto").isEmpty())) { //NOI18N
112 message.setHeader("Errors-To", this.properties.getProperty("mailer.errorsto")); //NOI18N#
115 // Is the property "bouncesto" set?
116 if ((this.properties.containsKey("mailer.bouncesto")) && (!this.properties.getProperty("mailer.bouncesto").isEmpty())) { //NOI18N
118 message.setHeader("Bounces-To", this.properties.getProperty("mailer.bouncesto")); //NOI18N
121 // Is the property "xloop" set?
122 if ((this.properties.containsKey("mailer.xloop")) && (!this.properties.getProperty("mailer.xloop").isEmpty())) { //NOI18N
124 message.setHeader("X-Loop", this.properties.getProperty("mailer.xloop")); //NOI18N
127 // Directly send email
128 Transport.send(message);
131 this.getLoggerBeanLocal().logTrace("deliverMail: EXIT!"); //NOI18N
135 * Sends given mail template to all addresses found in email wrapper
137 * @param template Template to send
138 * @param context Velocity context
139 * @param emailWrapper Email wrapper containing recipient and such
140 * @param mailSession Mail session
142 * @throws MessagingException If something happened on message delivery
144 protected void deliverMailWithTemplate (final Template template, final Context context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException {
146 this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMailWithTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
148 // The parameters must be valid
149 if (null == template) {
151 throw new NullPointerException("template is null"); //NOI18N
152 } else if (null == emailWrapper) {
154 throw new NullPointerException("emailWrapper is null"); //NOI18N
155 } else if (emailWrapper.getLocale() == null) {
157 throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
158 } else if (emailWrapper.getRecipientAddress() == null) {
160 throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
161 } else if (emailWrapper.getTemplateName() == null) {
163 throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
164 } else if (emailWrapper.getTemplateName().isEmpty()) {
166 throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
167 } else if (emailWrapper.getTemplateVariables() == null) {
169 throw new NullPointerException("emailWrapper.templateVariables is null"); //NOI18N
170 } else if (emailWrapper.getTemplateVariables().isEmpty()) {
172 throw new IllegalArgumentException("emailWrapper.templateVariables is empty"); //NOI18N
173 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
175 throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
176 } else if (null == mailSession) {
178 throw new NullPointerException("mailSession is null"); //NOI18N
181 // Get writer instance
182 StringWriter writer = new StringWriter();
185 template.merge(context, writer);
187 // Get all out and send it
188 this.deliverMail(emailWrapper, writer, mailSession);
191 this.getLoggerBeanLocal().logTrace("deliverMailWithTemplate: EXIT!"); //NOI18N
195 * Setter for initial properties
197 * @param properties Initial properties
199 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
200 protected void setProperties (final Properties properties) {
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setProperties: properties={0} - CALLED!", properties)); //NOI18N
204 // Are all required properties set?
205 if (null == properties) {
207 throw new NullPointerException("properties is null"); //NOI18N
208 } else if (!properties.containsKey("mailer.from")) { //NOI18N
210 throw new IllegalArgumentException("properties.mailer.from is not set"); //NOI18N
211 } else if (!properties.containsKey("mailer.errorsto")) { //NOI18N
213 throw new IllegalArgumentException("properties.mailer.errorsto is not set"); //NOI18N
214 } else if (!properties.containsKey("mailer.bouncesto")) { //NOI18N
215 // Bounces-To not set
216 throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N
220 this.properties = properties;
223 this.getLoggerBeanLocal().logTrace("setProperties: EXIT!"); //NOI18N
227 * Getter for template engine instance
229 * @return Template engine instance
231 protected VelocityEngine getTemplateEngine () {
232 return this.templateEngine;