View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: MailSender.java,v 1.1.1.1 2004/05/19 12:33:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.alert.sender;
21  import java.util.List;
22  import java.util.Iterator;
23  import java.util.Properties;
24  import javax.mail.Session;
25  import javax.mail.internet.MimeMessage;
26  import javax.mail.internet.InternetAddress;
27  import javax.mail.Transport;
28  import javax.mail.Address;
29  
30  import org.backsource.alert.Sender;
31  import org.backsource.alert.PropertyConstants;
32  import org.backsource.alert.PropertyUtil;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  /***
37   * Send arror and warnings by mail.
38   *
39   * <p>The sender uses the normal JavaMail properties for its sending, so any
40   * standard properties given i properties will reach the mailer. It also uses the properties {@link org.backsource.alert.PropertyConstants#MAIL_ERROR_TO} and {@link org.backsource.alert.PropertyConstants#MAIL_WARNING_TO} to send errors and warnings. Both are space separated lists of mail adresses.</p>
41   *
42   *
43   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
44   * @version $Revision: 1.1.1.1 $
45   */
46  
47  public class MailSender extends SenderBase{
48     static final Log log = LogFactory.getLog(MailSender.class);
49     String[] errTo = null;
50     String[] warnTo = null;
51     
52     public MailSender () throws Exception {
53     }
54  
55     protected void setUp() throws Exception {
56        //prop = PropertyUtil.getProperties();
57  
58        // Get mail addresses
59        List err = PropertyUtil.getValues(prop.getProperty(PropertyConstants.MAIL_ERROR_TO));
60        errTo = (String[])err.toArray(new String[ err.size()]);
61        List warn = PropertyUtil.getValues(prop.getProperty(PropertyConstants.MAIL_WARNING_TO));
62        warnTo = (String[])warn.toArray(new String[ warn.size()]);
63        
64     }
65     
66     public void sendErrors(List errors, List warnings) {
67        String e = "Instant News Errors:\n" + formatMessage(errors);
68        String w = "Instant News Warnings:\n" + formatMessage(warnings);
69        sendMail(errTo, "Instant News Error", e+w);
70     }
71     public void sendWarnings(List warnings) {
72        String w = "Instant News Errors:\n" + formatMessage(warnings);
73        sendMail(errTo, "Instant News Warnings", w);
74     }
75  
76     private String formatMessage(List messages) {
77        Iterator i = messages.iterator();
78        StringBuffer buf = new StringBuffer();
79        while (i.hasNext()) {
80           buf.append((String)i.next()).append("\n");
81        } // end of while ()
82        return buf.toString();
83     }
84     
85     private void sendMail(String[] to, String subject, String msg) {
86        try {
87           
88        
89           Session session = Session.getInstance(prop);
90           Address[] addresses = new Address[to.length];
91           for(int i = 0; i < to.length;i++)
92              addresses[i] = new InternetAddress(to[i]);        
93           MimeMessage m = new MimeMessage(session);
94           m.setFrom();
95           m.setRecipients(javax.mail.Message.RecipientType.TO, addresses);
96           m.setSubject(subject);
97           m.setSentDate(new java.util.Date());
98           m.setContent(msg, "text/plain");
99           Transport.send(m);
100       } catch (Exception e) {
101          log.error("Could not send mail to", e);
102       }
103    }
104 
105    public static void main(String[] args){
106       
107    }
108 } // MailSender