]> git.mxchange.org Git - jmailer-ee.git/blob - src/org/mxchange/jmailee/model/delivery/BaseMailer.java
this method was a bad idea anyway, to much unflexible
[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.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;
31
32 /**
33  * An email class for sending out mails from templates
34  * <p>
35  * @author Roland Haeder<roland@mxchange.org>
36  */
37 public abstract class BaseMailer implements DeliverableEmail {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 14_598_912_753_106L;
43
44         /**
45          * Logger bean
46          */
47         @Log
48         private LoggerBeanLocal loggerBeanLocal;
49
50         /**
51          * Default constructor
52          */
53         protected BaseMailer () {
54                 try {
55                         // Get initial context
56                         Context context = new InitialContext();
57
58                         // Lookup logger
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) {
61                         // Continue to throw
62                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
63                 }
64         }
65
66         /**
67          * Getter for logger bean
68          * <p>
69          * @return Local logger bean
70          */
71         protected LoggerBeanLocal getLoggerBeanLocal () {
72                 return this.loggerBeanLocal;
73         }
74
75         /**
76          * Sends an email to given email address with subject line.
77          * <p>
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
82          * <p>
83          * @throws NamingException If the resource cannot be found
84          * @throws MessagingException If something happened on message delivery
85          */
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);
89
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);
95
96                 // Directly send email
97                 Transport.send(message);
98         }
99
100 }