View Javadoc

1   package org.jmux.app.component;
2   
3   import org.jmux.Log;
4   import org.jmux.app.App;
5   import org.jmux.app.component.config.Config;
6   
7   import javax.mail.Message;
8   import javax.mail.MessagingException;
9   import javax.mail.Session;
10  import javax.mail.Transport;
11  import javax.mail.internet.InternetAddress;
12  import javax.mail.internet.MimeMessage;
13  import java.util.Properties;
14  
15  /******************************************************************************
16   jmux - Java Modules Using XML
17   Copyright © 2006 jmux.org
18  
19   This library is free software; you can redistribute it and/or
20   modify it under the terms of the GNU Lesser General Public
21   License as published by the Free Software Foundation; either
22   version 2.1 of the License, or (at your option) any later version.
23  
24   This library is distributed in the hope that it will be useful,
25   but WITHOUT ANY WARRANTY; without even the implied warranty of
26   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27   Lesser General Public License for more details.
28  
29   You should have received a copy of the GNU Lesser General Public
30   License along with this library; if not, write to the Free Software
31   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
32   *****************************************************************************/
33  
34  /***
35   * @author donaldw
36   */
37  public class MailSender extends AbstractComponent {
38  
39      private Session session;
40  
41      public void configure(Config config, App app) {
42          super.configure(config, app);
43  
44          Properties properties = new Properties();
45  
46          boolean debug = config.getBoolean(false, "debug");
47  
48          String hostString = config.getString("smtp1.betherenow.co.uk", "host");
49  
50          properties.put("mail.smtp.host", hostString);
51  
52          // create some properties and get the default Session
53          session = Session.getDefaultInstance(properties, null);
54          session.setDebug(debug);
55      }
56  
57      public boolean send(String from, String to, String subject, String body) {
58  
59          // create a message
60          Message msg = new MimeMessage(session);
61  
62          try {
63              // set the from and to address
64              InternetAddress addressFrom = new InternetAddress(from);
65              msg.setFrom(addressFrom);
66  
67              msg.setRecipients(Message.RecipientType.TO,
68                      new InternetAddress[]
69                              {
70                                      new InternetAddress(to)
71                              });
72  
73              // Setting the Subject and Content Type
74              msg.setSubject(subject);
75              msg.setContent(body, "text/html");
76  
77              Transport.send(msg);
78          } catch (MessagingException e) {
79              Log.error("Could not send message: " + e);
80              return true;
81          }
82          return false;
83      }
84  }