View Javadoc

1   package org.wcb.autohome.util;
2   
3   import org.wcb.autohome.interfaces.IX10MonitorEvent;
4   import org.wcb.autohome.implementations.EmailHeaderBean;
5   
6   import java.io.UnsupportedEncodingException;
7   import java.util.Properties;
8   import java.util.Locale;
9   import java.util.Calendar;
10  import java.util.GregorianCalendar;
11  import java.text.SimpleDateFormat;
12  
13  import javax.mail.*;
14  import javax.mail.internet.MimeMessage;
15  import javax.mail.internet.InternetAddress;
16  import javax.mail.internet.AddressException;
17  
18  /***
19   * Copyright (C) 1999  Walter Bogaardt
20   *
21   * This library is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU Lesser General Public
23   * License as published by the Free Software Foundation; either
24   * version 2 of the License, or (at your option) any later version.
25   *
26   * This library is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   * Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public
32   * License along with this library; if not, write to the Free Software
33   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
34   *
35   * EmailService.java
36   *
37   * Created on January 19, 2004, 7:59 PM
38   *
39   * @author  wbogaardt
40   *
41   *  Project:   Home Automation Interface
42   *  Filename:  $Id: EmailService.java,v 1.5 2004/02/02 23:41:31 wbogaardt Exp $
43   *  Abstract:  This allows the alice interface to send off email notification of monitored events.
44   *
45   *  $Log: EmailService.java,v $
46   *  Revision 1.5  2004/02/02 23:41:31  wbogaardt
47   *  refactored email so that properties are maintained in bean object and added allowing username and password authentication for email servers that require it.
48   *
49   *  Revision 1.4  2004/01/20 00:38:13  wbogaardt
50   *  removed dead import
51   *
52   *  Revision 1.3  2004/01/20 00:20:58  wbogaardt
53   *  allow email service to send status code back if email succeds or fails
54   *
55   *  Revision 1.2  2004/01/19 22:35:38  wbogaardt
56   *  added fixes to runing events and emails so they work and added a testing of a monitored event through the table popup on a right mouse click.
57   *
58   */
59  public class EmailService {
60  
61      public static final String NAME = "name";
62      public static final String SUBJECT = "subject";
63      public static final String EMAIL = "email";
64      private Session SESSION;
65      private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:mm:ss a", new Locale("en","US"));
66      private String sHost;
67      private Authenticator aAuthorize;
68      private EmailHeaderBean emailBean;
69  
70      public EmailService(EmailHeaderBean eBean){
71          this.emailBean = eBean;
72          this.sHost = eBean.getEmailHost();
73          this.aAuthorize = eBean.getEmailAuthenticator();
74          Properties property = new Properties();
75          property.put("mail.smtp.host", sHost);
76          if(this.aAuthorize != null)
77          {
78              property.put("mail.smtp.auth","true");
79          }
80          SESSION =  Session.getDefaultInstance(property, aAuthorize);
81      }
82  
83      /***
84       * This takes an X10MonitorEvent object and sends a message about this monitor
85       * to the user specified email address.
86       * @param ev Monitored X10MonitorEvent object.
87       * @return true success and false failed sending email.
88       */
89      public boolean sendEventMessage(IX10MonitorEvent ev){
90          MimeMessage message = this.createMessage(this.createBody(ev));
91          return this.sendMessage(message);
92      }
93  
94      /***
95       * This takes the message and sends it out to the email server.
96       * @param message The feedback message
97       * @return true indicates success false is failure because of handled exceptions.
98       */
99      private boolean sendMessage(MimeMessage message)
100     {
101         try
102         {
103             Transport.send(message);
104         }
105         catch(NoSuchProviderException nspe)
106         {
107             System.err.println("Failed to send email message " + nspe);
108             return false;
109         }
110         catch(MessagingException me)
111         {
112             System.err.println("Failed to send email message " + me);
113             return false;
114         }
115         return true;
116     }
117 
118     /***
119      * Attempts to create the message from the GenericEmail object, which
120      * is persistable because of the JDO implementation.
121      *
122      * @param sMessageBody Main body of the message
123      * @return Complete MimeMessage ready to be sent to feedback@directv.com
124      */
125     private MimeMessage createMessage(String sMessageBody){
126         MimeMessage message = new MimeMessage(SESSION);
127         // Construct a message
128         try{
129             message.setFrom(new InternetAddress(this.emailBean.getEmailFromAddress(), "Alice"));
130             message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.emailBean.getEmailToAddress()));
131             message.setSubject("Event detected at home");
132             message.setText(sMessageBody);
133         }
134         catch(AddressException ae)
135         {
136             System.err.println("Bad Email address " + ae);
137         }
138         catch(MessagingException me)
139         {
140             System.err.println("Bad Email address " + me);
141         }
142         catch(UnsupportedEncodingException uex)
143         {
144             System.err.println("Bad Email address " + uex);
145         }
146         return message;
147     }
148 
149     /***
150      * This creates the basic email body which will be sent to Feedback@directv.com, which the call centers will
151      * service through kana.
152      * @param module X10Monitor module
153      * @return  formated message
154      */
155     private String createBody(IX10MonitorEvent module){
156         StringBuffer sbBuff = new StringBuffer("\n\n The following modules detected an event and are notifying you.\n\n");
157         sbBuff.append("Module: " + module.getMonitoringModule().getFullDeviceCode());
158         sbBuff.append(" - " + module.getDescription() + " at " + module.getLocation());
159         sbBuff.append("\n detected an event at " + this.getDateTime(module.getTimeDetected()));
160         return sbBuff.toString();
161     }
162 
163     /***
164      * Take a calendar and parse it to a string that the user can understand in an email.
165      * @param cal Date and time stamp
166      * @return String printable date in yyyy/MM/dd h:mm:ss a format
167      */
168     private String getDateTime(Calendar cal){
169         if(cal == null)
170         {
171             GregorianCalendar calendar = new GregorianCalendar();
172             return sdf.format(calendar.getTime());
173         }
174         String output = sdf.format(cal.getTime());
175         return output;
176     }
177 }