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.jms.MessageProducer;
22 import javax.jms.ObjectMessage;
23 import javax.mail.MessagingException;
24 import javax.mail.Session;
25 import javax.mail.Transport;
26 import javax.mail.internet.InternetAddress;
27 import javax.mail.internet.MimeMessage;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
32 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
33 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
36 * An email class for sending out mails from templates
38 * @author Roland Haeder<roland@mxchange.org>
40 public abstract class BaseMailer implements DeliverableEmail {
45 private static final long serialVersionUID = 14_598_912_753_106L;
51 private LoggerBeanLocal loggerBeanLocal;
56 protected BaseMailer () {
58 // Get initial context
59 Context context = new InitialContext();
62 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
63 } catch (final NamingException ex) {
65 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
70 public void sendEmailChangeMail (final MessageProducer messageProducer, final ObjectMessage message, final ChangeableEmailAddress emailChange) {
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmailChangeMail: messageProducer={0},message={1},emailChange={2} - CALLED", messageProducer, message, emailChange)); //NOI18N
74 // All parameters + some sub objects must be set
75 if (null == messageProducer) {
77 throw new NullPointerException("messageProducer is null"); //NOI18N
78 } else if (null == message) {
80 throw new NullPointerException("message is null"); //NOI18N
81 } else if (null == emailChange) {
83 throw new NullPointerException("emailChange is null"); //NOI18N
84 } else if (emailChange.getEmailChangeUser() == null) {
86 throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
87 } else if (emailChange.getEmailChangeUser().getUserId() == null) {
89 throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
90 } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
92 throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
93 } else if (emailChange.getEmailChangeUser().getUserContact() == null) {
95 throw new NullPointerException("emailChange.emailChangeUser.userContact is null"); //NOI18N
96 } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() == null) {
98 throw new NullPointerException("emailChange.emailChangeUser.userContact.contactId is null"); //NOI18N
99 } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() < 1) {
101 throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userContact.contactId={0} is invalid.", emailChange.getEmailChangeUser().getUserContact().getContactId())); //NOI18N
102 } else if (emailChange.getEmailAddress().trim().isEmpty()) {
103 // Email address is empty
104 throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N
107 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
111 * Getter for logger bean
113 * @return Local logger bean
115 protected LoggerBeanLocal getLoggerBeanLocal () {
116 return this.loggerBeanLocal;
120 * Sends an email to given email address with subject line.
122 * @param emailAddress Email address for recipient
123 * @param subjectLine Subject line
124 * @param body Body part
125 * @param mailSession Corresponding mail session to use
127 * @throws NamingException If the resource cannot be found
128 * @throws MessagingException If something happened on message delivery
130 protected void sendMail (final String emailAddress, final String subjectLine, final String body, final Session mailSession) throws NamingException, MessagingException {
131 // Get MIME message instance
132 MimeMessage message = new MimeMessage(mailSession);
134 // Set subject, recipients and body
135 message.setSubject(subjectLine);
136 message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
137 message.setSentDate(new Date());
138 message.setText(body);
140 // Directly send email
141 Transport.send(message);