1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package net.codesmarts.log4j;
32 import org.apache.commons.mail.EmailException;
33 import org.apache.commons.mail.HtmlEmail;
34 import org.apache.commons.mail.SimpleEmail;
35
36 /***
37 * Base class for appenders that file bug reports as emails to specified addresses
38 * @author Fred McCann
39 */
40 public abstract class AbstractEmailBugReportAppender extends AbstractBugReportAppender {
41
42 private String to;
43 private String from;
44 private String smtpServer;
45 private String subjectPrepend = "";
46 private boolean html = false;
47
48 /***
49 * Implementing class decides how to create email message body
50 * @param report
51 * @return
52 */
53 public abstract String getMessage(BugReport report);
54
55 /***
56 * Implementing class decides how to create email message body
57 * @param report
58 * @return
59 */
60 public String getHTMLMessage(BugReport report) {
61 return null;
62 }
63
64 /***
65 * Implementing class decides how to create email subject
66 * @param report
67 * @return
68 */
69 public abstract String getSubject(BugReport report);
70
71 /***
72 * @see net.codesmarts.log4j.BugReportAppender#append(net.codesmarts.log4j.BugReport)
73 */
74 public synchronized void append(BugReport report) {
75 String[] addresses = to.split(",");
76 String subject = subjectPrepend+getSubject(report);
77
78 for (int x = 0; x < addresses.length; x++) {
79 if (html==true) {
80 try {
81 HtmlEmail email = new HtmlEmail();
82 email.setHostName(smtpServer);
83 email.setFrom(from);
84 email.addTo(addresses[x]);
85 email.setSubject(subject);
86
87 String htmlMessage = getHTMLMessage(report);
88 if (htmlMessage!=null)
89 email.setHtmlMsg(htmlMessage);
90 email.setMsg(getMessage(report));
91
92 email.send();
93 }
94 catch (EmailException e) {
95 getErrorHandler().error("Could not send message: "+subject+" to: "+
96 addresses[x]+" from: "+from+". "+e.getMessage());
97 }
98 }
99 else {
100 try {
101 SimpleEmail email = new SimpleEmail();
102 email.setHostName(smtpServer);
103 email.setFrom(from);
104 email.addTo(addresses[x]);
105 email.setSubject(subject);
106 email.setMsg(getMessage(report));
107 email.send();
108 }
109 catch (EmailException e) {
110 getErrorHandler().error("Could not send message: "+subject+" to: "+
111 addresses[x]+" from: "+from+". "+e.getMessage());
112 }
113 }
114 }
115 }
116
117 /***
118 * @see net.codesmarts.log4j.AbstractBugReportAppender#init()
119 */
120 public void init() {
121
122 boolean good = true;
123
124 if (smtpServer==null) {
125 getErrorHandler().error("Must set SMTP server for "+this.getClass().getName());
126 good = false;
127 }
128
129 if (from==null) {
130 getErrorHandler().error("Must set \"from\" email address for "+this.getClass().getName());
131 good = false;
132 }
133
134 if (to==null) {
135 getErrorHandler().error("Must set \"to\" email address for "+this.getClass().getName());
136 good = false;
137 }
138
139 if (good=false)
140 close();
141 }
142
143 /***
144 * Set the email address from which to send messages
145 * @param from The from to set.
146 */
147 public void setFrom(String from) {
148 this.from = from;
149 }
150
151 /***
152 * Set the SMTP server to use for message delivery
153 * @param smtpServer The smtpServer to set.
154 */
155 public void setSmtpServer(String smtpServer) {
156 this.smtpServer = smtpServer;
157 }
158
159 /***
160 * Set comma delimited list of message recipients
161 * @param to The to to set.
162 */
163 public void setTo(String to) {
164 this.to = to;
165 }
166
167 /***
168 * Set this to true to send messages in HTML format; defaults to false
169 * @param html The html to set.
170 */
171 public void setHtml(boolean html) {
172 this.html = html;
173 }
174
175 /***
176 * Set optional string to prepend to all messages
177 * @param subjectPrepend The subjectPrepend to set.
178 */
179 public void setSubjectPrepend(String subjectPrepend) {
180 this.subjectPrepend = subjectPrepend;
181 }
182 }