]> git.mxchange.org Git - jmailer-ee.git/blob - src/org/mxchange/jmailee/model/delivery/BaseMailer.java
The exception's message now contains the thrown exception's message, too.
[jmailer-ee.git] / src / org / mxchange / jmailee / model / delivery / BaseMailer.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jmailee.model.delivery;
18
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;
34
35 /**
36  * An email class for sending out mails from templates
37  * <p>
38  * @author Roland Haeder<roland@mxchange.org>
39  */
40 public abstract class BaseMailer implements DeliverableEmail {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 14_598_912_753_106L;
46
47         /**
48          * Logger bean
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Default constructor
55          */
56         protected BaseMailer () {
57                 try {
58                         // Get initial context
59                         Context context = new InitialContext();
60
61                         // Lookup logger
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) {
64                         // Continue to throw
65                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
66                 }
67         }
68
69         @Override
70         public void sendEmailChangeMail (final MessageProducer messageProducer, final ObjectMessage message, final ChangeableEmailAddress emailChange) {
71                 // Trace message
72                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmailChangeMail: messageProducer={0},message={1},emailChange={2} - CALLED", messageProducer, message, emailChange)); //NOI18N
73
74                 // All parameters + some sub objects must be set
75                 if (null == messageProducer) {
76                         // Throw NPE
77                         throw new NullPointerException("messageProducer is null"); //NOI18N
78                 } else if (null == message) {
79                         // Throw NPE again
80                         throw new NullPointerException("message is null"); //NOI18N
81                 } else if (null == emailChange) {
82                         // ... and again ...
83                         throw new NullPointerException("emailChange is null"); //NOI18N
84                 } else if (emailChange.getEmailChangeUser() == null) {
85                         // Throw NPE again
86                         throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
87                 } else if (emailChange.getEmailChangeUser().getUserId() == null) {
88                         // Throw NPE again
89                         throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
90                 } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
91                         // Not valid id
92                         throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
93                 } else if (emailChange.getEmailChangeUser().getUserContact() == null) {
94                         // Throw NPE again
95                         throw new NullPointerException("emailChange.emailChangeUser.userContact is null"); //NOI18N
96                 } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() == null) {
97                         // Throw NPE again
98                         throw new NullPointerException("emailChange.emailChangeUser.userContact.contactId is null"); //NOI18N
99                 } else if (emailChange.getEmailChangeUser().getUserContact().getContactId() < 1) {
100                         // Not valid id
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
105                 }
106
107                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
108         }
109
110         /**
111          * Getter for logger bean
112          * <p>
113          * @return Local logger bean
114          */
115         protected LoggerBeanLocal getLoggerBeanLocal () {
116                 return this.loggerBeanLocal;
117         }
118
119         /**
120          * Sends an email to given email address with subject line.
121          * <p>
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
126          * <p>
127          * @throws NamingException    If the resource cannot be found
128          * @throws MessagingException If something happened on message delivery
129          */
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);
133
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);
139
140                 // Directly send email
141                 Transport.send(message);
142         }
143
144 }