2
package org.crosswire.mail;
4
import java.io.PrintWriter;
5
import java.io.StringWriter;
7
import java.util.Properties;
9
import javax.mail.Address;
10
import javax.mail.Message;
11
import javax.mail.MessagingException;
12
import javax.mail.SendFailedException;
13
import javax.mail.Session;
14
import javax.mail.Transport;
15
import javax.mail.internet.InternetAddress;
16
import javax.mail.internet.MimeMessage;
18
import org.crosswire.util.Level;
19
import org.crosswire.util.Logger;
20
import org.crosswire.util.Reporter;
21
import org.crosswire.util.event.ReporterEvent;
22
import org.crosswire.util.event.ReporterListener;
25
* This class listens to Log captures and copies them to a
28
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
29
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
30
* Distribution Licence:<br />
31
* Project B is free software; you can redistribute it
32
* and/or modify it under the terms of the GNU General Public License,
33
* version 2 as published by the Free Software Foundation.<br />
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37
* General Public License for more details.<br />
38
* The License is available on the internet
39
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
40
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
41
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
42
* The copyright to this program is held by it's authors.
43
* </font></td></tr></table>
44
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
45
* @see <{docs.Licence}>
48
public class MailCaptureListener implements ReporterListener
51
* Called whenever log.capture() is passed an Exception
52
* @param ev Object describing the exception
54
public void reportException(ReporterEvent ev)
56
// Create a string from the exception
57
StringWriter str = new StringWriter();
58
ev.getException().printStackTrace(new PrintWriter(str));
59
String message = new String(str.getBuffer());
61
sendMail("Exception from "+ev.getSourceName(), message);
65
* Called whenever log.capture() is passed an Exception
66
* @param ev Object describing the exception
68
public void reportMessage(ReporterEvent ev)
70
sendMail("Message from "+ev.getSourceName(), ev.getMessage());
74
* Called whenever log.capture() is passed an Exception
75
* @param ev Object describing the exception
77
public void sendMail(String subject, String body)
79
// create some properties and get the default Session
80
Properties props = new Properties();
81
props.put("mail.smtp.host", mail_server);
82
props.put("mail.smtp.port", ""+mail_port);
84
Session session = Session.getDefaultInstance(props, null);
88
props.put("mail.debug", "true");
89
session.setDebug(mail_debug);
94
// Create the basic message
95
MimeMessage msg = new MimeMessage(session);
96
msg.setFrom(new InternetAddress(mail_fromaddr));
97
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail_toaddr, false));
98
msg.setSubject(subject);
99
msg.setSentDate(new Date());
100
msg.setHeader("X-Mailer", "MailCaptureListener");
101
msg.setText(body /*default charset*/);
104
// Create and fill the second message part
105
MimeBodyPart mbp = new MimeBodyPart();
106
mbp.setText(message, "us-ascii");
108
// Create the Multipart and its parts to it
109
Multipart mp = new MimeMultipart();
112
// Add the Multipart to the message
119
catch (MessagingException ex)
121
// log.fine("Warning failure in MailException reporting. Turning Mail reporting off.");
122
//if (ex instanceof MessagingException)
124
printMessagingException(ex);
125
throw new IllegalAccessError("Mail system failed. See system output for details.");
130
log.log(Level.INFO, "Failure during reporting", ex);
133
// Turn ourselves off and then throw again.
134
// Is this a good idea? What if there is more than one thing that
135
// handles Exceptions? Hmmmm.
136
setHelpDeskListener(false);
137
log.capture(this, new Exception("Mail system failed. See system output for details. Disabling Mail."));
143
* You must call getHelpDeskListener() in order to start displaying
144
* Exceptions sent to the Log, and in order to properly
145
* close this class you must call it again (with false).
146
* @param joined Are we listening to the Log
148
public static void setHelpDeskListener(boolean joined)
150
if (joined && li == null)
152
li = new MailCaptureListener();
153
Reporter.addReporterListener(li);
156
if (!joined && li != null)
158
Reporter.removeReporterListener(li);
164
* Get the listening status
166
public static boolean getHelpDeskListener()
172
* Some debug for when we get an exception
173
* @param out The stream to write some debug to
174
* @param ex The MessagingException to print
176
private static void printMessagingException(Exception ex)
178
log.log(Level.INFO, "Mail Exception", ex);
180
if (ex instanceof SendFailedException)
182
SendFailedException sfex = (SendFailedException) ex;
184
Address[] invalid = sfex.getInvalidAddresses();
187
log.info("Invalid Addresses:");
188
for (int i=0; i<invalid.length; i++)
189
log.info(" "+invalid[i]);
192
Address[] unsent = sfex.getValidUnsentAddresses();
195
log.info("Unsent Addresses");
196
for (int i=0; i<unsent.length; i++)
197
log.info(" "+unsent[i]);
200
Address[] sent = sfex.getValidSentAddresses();
203
log.info("Sent Addresses");
204
for (int i=0; i<sent.length; i++)
205
log.info(" "+sent[i]);
209
if (ex instanceof MessagingException)
211
printMessagingException(((MessagingException) ex).getNextException());
216
* E-Mail - the from username
218
public static boolean getMailDebug()
224
* E-Mail - the from username
226
public static void setMailDebug(boolean value)
232
* E-Mail - the from address
234
public static String getMailFromAddr()
236
return mail_fromaddr;
240
* E-Mail - the from address
242
public static void setMailFromAddr(String value)
244
mail_fromaddr = value;
248
* E-Mail - the to address
250
public static String getMailToAddr()
256
* E-Mail - the to address
258
public static void setMailToAddr(String value)
264
* E-Mail - the server name
266
public static String getMailServer()
272
* E-Mail - the server name
274
public static void setMailServer(String value)
280
* E-Mail - the server port
282
public static int getMailPort()
288
* E-Mail - the server port
290
public static void setMailPort(int value)
295
/** E-Mail - the from username */
296
private static boolean mail_debug = false;
298
/** E-Mail - the from address */
299
private static String mail_fromaddr = "source@nowhere.com";
301
/** E-Mail - the to address */
302
private static String mail_toaddr = "dest@nowhere.com";
304
/** E-Mail - the server name */
305
private static String mail_server = "smtp";
307
/** E-Mail - the server port */
308
private static int mail_port = 25;
310
/** The listener that pops up the ExceptionPanes */
311
private static MailCaptureListener li = null;
313
/** The log stream */
314
protected static Logger log = Logger.getLogger(MailCaptureListener.class);