Reputation: 4289
can i trigger an email with attachment(log files) using the log4j.smtpAppender.
I'm currently using this appender to trigger emails for error and fatal level exceptions. Can I add the log file in the same email as attachment
log4j.appender.email=org.apache.log4j.net.SMTPAppender
Upvotes: 0
Views: 1326
Reputation: 4289
public static void emailAttachment
throws AddressException, MessagingException{
String host = mail.company.com;
String from = [email protected];
String to = [email protected];
String cc = [email protected];
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setSubject("Email Notification");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("email Body");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("attachment.pdf");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send( message );
}
Source: jGuru.com
Upvotes: 1
Reputation: 2975
I think you can't send a log file in the same email. You can of course configure several appenders to log your data: example one sending email (SMTPAppender), other printing to stdout (ConsoleAppender), etc.
Besides, I don't think it is a good idea to attach a log file to the same email: the log file will keep growing each time a new email is sent, and suppose your log is about 5MB long...then logging will eat you a big chunk of processing power.
Upvotes: 1