|
Files are attached. Just change the way of getting SMTP. And log4j.properties log4j.appender.A3=org.pernica.util.Log4jMailAppender [EMAIL PROTECTED] [EMAIL PROTECTED] log4j.appender.A3.subject=Zpráva serveru o FAXER log4j.appender.A3.location=http://hefaistos:8080/faxer log4j.appender.A3.layout=org.apache.log4j.PatternLayout log4j.appender.A3.threshold=ERROR # Print the date in ISO 8601 format log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.A3.layout.ConversionPattern=%d [%t] %-5p %c - %m%n Regards Jan utpal wrote: I m not getting you... can you give me an example ? Thanks, UtpalOn Thu, 2005-07-07 at 17:30, Jan Pernica wrote: |
package org.pernica.util;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
import java.io.*;
/**
* Trida po posilani emailu
*
* @author Jakub Dadak <[EMAIL PROTECTED]>
* @version 0.9, 26.5.2001
*/
public class Mail {
protected static org.apache.log4j.Logger log4j =
org.apache.log4j.Logger.getLogger(Mail.class);
/** Metoda pro poslani jednoho mailu. Haze exception s pricinou pripadne
chyby */
public static void sendMail(String smtpServer, String from, String to,
String subjectIn, String message) {
sendMail(smtpServer, from, to, subjectIn, message, "text/html");
} //end of sendMail
/** Metoda pro poslani jednoho mailu. Haze exception s pricinou pripadne
chyby, v parametru se zada take format mailu napr. text/html nebo text/plain */
public static void sendMail(String smtpServer, String from, String to,
String subjectIn, String message, String mailFormat) {
sendMail(smtpServer, from, to, null, null, subjectIn, message,
mailFormat);
}
public static void sendMail(String smtpServer, String from, String to,
String cc, String bc, String subjectIn, String message, String mailFormat) {
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
// Session session = Session.getDefaultInstance(props, null);
Session session = Session.getInstance(props, null);
session.setDebug(false);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address;
address = InternetAddress.parse(to, false);
msg.setRecipients(Message.RecipientType.TO, address);
if (cc != null) {
address = InternetAddress.parse(cc, false);
msg.setRecipients(Message.RecipientType.CC, address);
}
if (bc != null) {
address = InternetAddress.parse(bc, false);
msg.setRecipients(Message.RecipientType.BCC, address);
}
msg.setSubject(subjectIn, "ISO-8859-2");
msg.setSentDate(new java.util.Date());
// If the desired charset is known, you can use
// setText(text, charset)
msg.setContent(message, mailFormat + ";charset=\"iso-8859-2\"");
Transport.send(msg);
} catch (MessagingException mex) {
StringBuffer sb = new StringBuffer();
System.out.println("Exception: " + mex.getMessage());
mex.printStackTrace();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
sb.append("Chybné adresy: ");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
sb.append(invalid[i]+" ");
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
sb.append("Neodesláno: ");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
sb.append(validUnsent[i]+" ");
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
sb.append("Odesláno: ");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
sb.append(validSent[i]+" ");
}
}
}
}
while ((ex = ((MessagingException)ex).getNextException()) != null);
// log4j.error("MAIL NOT SENT: "+sb, mex);
throw new MailException(sb.toString());
} // End of catch
} //end of sendMail
/** Method for sending e-mail with atachments. Atachments are as list of
MimeBodyPart */
public static void sendMail(String smtpServer, String from, String to,
String subject, ArrayList mimeBodyParts) {
sendMail(smtpServer, from, to, null, null, subject, mimeBodyParts);
}
/** Method for sending e-mail with atachments. Atachments are as list of
MimeBodyPart */
public static void sendMail(String smtpServer, String from, String to,
String cc, String bc, String subject, ArrayList mimeBodyParts) {
boolean debug = false; // only for tests
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = InternetAddress.parse(to, false);
msg.setRecipients(Message.RecipientType.TO, address);
if (cc != null) {
address = InternetAddress.parse(cc, false);
msg.setRecipients(Message.RecipientType.CC, address);
}
if (bc != null) {
address = InternetAddress.parse(bc, false);
msg.setRecipients(Message.RecipientType.BCC, address);
}
msg.setSubject(subject,"iso-8859-2");
msg.setSentDate(new Date());
Multipart mp = new MimeMultipart();
for (int i=0; i<mimeBodyParts.size(); i++) {
mp.addBodyPart((MimeBodyPart)mimeBodyParts.get(i));
}
// add the Multipart to the message
msg.setContent(mp);
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
StringBuffer sb = new StringBuffer();
System.out.println("Exception: " + mex.getMessage());
mex.printStackTrace();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
sb.append("Chybné adresy: ");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
sb.append(invalid[i]+" ");
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
sb.append("Nebylo doruèeno: ");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
sb.append(validUnsent[i]+" ");
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
sb.append("Bylo posláno: ");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
sb.append(validSent[i]+" ");
}
}
}
}
while ((ex = ((MessagingException)ex).getNextException()) != null);
// log4j.error("MAIL NOT SENT: "+sb, mex);
throw new MailException(sb.toString());
} // End of catch
}
public static class MailException extends RuntimeException {
public MailException( String message ) {
super(message);
}
}
} // end of class Mailer
/*
* Log4jMailAppender.java
*
* Created on 11. duben 2005, 9:44
*/
package org.pernica.util;
import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import org.apache.log4j.helpers.*;
/**
* Log4j appender class used to send e-mail messages
* of events recieved. This appender should always be added
* to the log4j provided AsynchronousAppender becuase of the
* overhead involved in sending e-mail messages.
*
* Sample log4j xml configuration: <code>
* <appender name="MAIL" = class="com.frontiers.server.mail.MailAppender">
* <param name="Threshold" value="ERROR" />
*
* <param name="To" value="[EMAIL PROTECTED]" />
* <param name="Subject" value="Error from = CaseServer!!" />
* <param name="From" value="[EMAIL PROTECTED]" />
* <param name="Location" value="CaseServer at Inova Fairfax. I.P.
= address: 10.1.1.44" />
*
* <layout class="org.apache.log4j.PatternLayout">
* <param name="ConversionPattern" = value="%d{ISO8601} %-5p
%c{1} - %m%n"/>
* </layout>
* </appender> </code>
*
* @author Jan Pernica, Copyright 2005 Pernica IT Solutions.
*/
public class Log4jMailAppender extends AppenderSkeleton {
/** From */
protected String _from;
/** Message recipients. Comma separated list. */
protected String _to;
/** E-mail subject to use */
protected String _subject;
/** Location of originating service */
protected String _location;
/** Default constructor. Does nothing. */
public Log4jMailAppender() {
}
/**
* Instantiates with layout to use for formatting
* messages.
*
* @param Layout - Layout to use
*/
public Log4jMailAppender(Layout layout) {
this.layout = layout;
}
public void setFrom(String from) { _from = from; }
public String getFrom() { return _from; }
public void setTo(String to) { _to = to; }
public String getTo() { return _to; }
public void setSubject(String subject) { _subject = subject; }
public String getSubject() { return _subject; }
public void setLocation(String loc) { _location = loc; }
public String getLocation() { return _location; }
/**
* Implementation of message appends. These
* mesages will be e-mail to recipients.
*
* @param event
*/
public void append(LoggingEvent event) {
try {
String smtp = (String) org.pernica.util.JNDI.get("mail/smtp
server");
String from = (String) org.pernica.util.JNDI.get("mail/sender");
if (smtp != null && from != null) {
//If we have a throwable string, capture and print it
if (event.getThrowableStrRep() != null &&
event.getThrowableStrRep().length > 0) {
StringBuffer str = new StringBuffer();
String[] s = event.getThrowableStrRep();
for (int i=0; i < s.length; i++) {
str.append(s[i]).append(Layout.LINE_SEP);
}
// System.out.println("sending mail to "+_to+":
"+this.layout.format(event));
org.pernica.util.Mail.sendMail(smtp,from,
_to, _subject, "Location:
"+_location+"\n"+this.layout.format(event)+"\n"+ str.toString());
} else {
//Regular generic message
// System.out.println("sending mail to "+_to+":
"+this.layout.format(event));
org.pernica.util.Mail.sendMail(smtp,from,
_to, _subject, "\nLocation:
"+_location+"\n"+this.layout.format(event));
}
}
} catch (Throwable t) {
System.out.println("MailAppender: Caught exception:" +
t.getMessage());
t.printStackTrace();
}
}
public void close(){}
public boolean requiresLayout() {
return true;
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
