1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
57
58
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 }
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 }