]> git.mxchange.org Git - jmailer-ee.git/commitdiff
added from jjobs-ejb as this is generic code
authorRoland Haeder <roland@mxchange.org>
Mon, 14 Mar 2016 21:47:22 +0000 (22:47 +0100)
committerRoland Haeder <roland@mxchange.org>
Mon, 14 Mar 2016 21:47:22 +0000 (22:47 +0100)
src/org/mxchange/jmailee/model/delivery/Mailer.java

index c14a395f57eb45a3746adcfa26b5df69c13c26cc..5af60a2d1dd9d1b9de9e399b6fea515d0b93c189 100644 (file)
 package org.mxchange.jmailee.model.delivery;
 
 import java.text.MessageFormat;
+import java.util.Date;
+import javax.annotation.Resource;
 import javax.jms.MessageProducer;
 import javax.jms.ObjectMessage;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
@@ -38,6 +45,12 @@ public class Mailer implements DeliverableEmail {
         */
        private static final long serialVersionUID = 14_598_912_753_106L;
 
+       /**
+        * Email session
+        */
+       @Resource (name = "jmail/jjobs")
+       private Session jmailjjobs;
+
        /**
         * Logger bean
         */
@@ -101,4 +114,28 @@ public class Mailer implements DeliverableEmail {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
 
+       /**
+        * Sends an email to given email address with subject line.
+        * <p>
+        * @param emailAddress Email address for recipient
+        * @param subjectLine  Subject line
+        * @param body         Body part
+        * <p>
+        * @throws NamingException    If the resource cannot be found
+        * @throws MessagingException If something happened on message delivery
+        */
+       private void sendMail (final String emailAddress, final String subjectLine, final String body) throws NamingException, MessagingException {
+               // Get MIME message instance
+               MimeMessage message = new MimeMessage(this.jmailjjobs);
+
+               // Set subject, recipients and body
+               message.setSubject(subjectLine);
+               message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
+               message.setSentDate(new Date());
+               message.setText(body);
+
+               // Directly send email
+               Transport.send(message);
+       }
+
 }