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.text.MessageFormat;
20 import java.util.Date;
21 import javax.mail.MessagingException;
22 import javax.mail.Session;
23 import javax.mail.Transport;
24 import javax.mail.internet.InternetAddress;
25 import javax.mail.internet.MimeMessage;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
33 * An email class for sending out mails from templates
35 * @author Roland Haeder<roland@mxchange.org>
37 public abstract class BaseMailer implements DeliverableEmail {
42 private static final long serialVersionUID = 14_598_912_753_106L;
48 private LoggerBeanLocal loggerBeanLocal;
53 protected BaseMailer () {
55 // Get initial context
56 Context context = new InitialContext();
59 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
60 } catch (final NamingException ex) {
62 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
67 * Getter for logger bean
69 * @return Local logger bean
71 protected LoggerBeanLocal getLoggerBeanLocal () {
72 return this.loggerBeanLocal;
76 * Sends an email to given email address with subject line.
78 * @param emailAddress Email address for recipient
79 * @param subjectLine Subject line
80 * @param body Body part
81 * @param mailSession Corresponding mail session to use
83 * @throws NamingException If the resource cannot be found
84 * @throws MessagingException If something happened on message delivery
86 protected void sendMail (final String emailAddress, final String subjectLine, final String body, final Session mailSession) throws NamingException, MessagingException {
87 // Get MIME message instance
88 MimeMessage message = new MimeMessage(mailSession);
90 // Set subject, recipients and body
91 message.setSubject(subjectLine);
92 message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
93 message.setSentDate(new Date());
94 message.setText(body);
96 // Directly send email
97 Transport.send(message);