MailLog is used to send out emails with/without attachments. It is particularly useful in scenarios wherein the application needs to send out an email to people specifying the error/warn/info messages and also send the log files as attachments for further debugging.
/* MailLog */ import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; /** * MailLog will create a multipart message with the second * block of the message being the given file.* * This demonstrates how to use the FileDataSource to send * a file via mail.
* * usage:
java MailLog to from smtp file true|false
* where to and from are the destination and * origin email addresses, respectively, and smtp * is the hostname of the machine that has smtp server * running. file is the file to send. The next parameter * either turns on or turns off debugging during sending. * */ /** Method sendMailWithoutAttachment() to send mails without attachment */ public class MailLog { private String mailTo[] = null; private String mailFrom = null; private String host = null; private String attachmentFileName = null; private String subject = null; private String msgText1 = null; private boolean debugFlag = true; public MailLog( String[] theMailTo, String theMailFrom, String theHost, String theAttachmentFileName, String theSubject, String theMsgText1, boolean theDebugFlag) { mailTo = theMailTo; mailFrom = theMailFrom; host = theHost; attachmentFileName = theAttachmentFileName; subject = theSubject; msgText1 = theMsgText1; debugFlag = theDebugFlag; } /** * Constructor to send mails without attachment. */ public MailLog( String[] theMailTo, String theMailFrom, String theHost, String theSubject, String theMsgText1, boolean theDebugFlag) { mailTo = theMailTo; mailFrom = theMailFrom; host = theHost; subject = theSubject; msgText1 = theMsgText1; debugFlag = theDebugFlag; } public void setMailTo(String[] theMailTo) { mailTo = theMailTo; } public void setMailFrom(String theMailFrom) { mailFrom = theMailFrom; } public void setHost(String theHost) { host = theHost; } public void setAttachmentFileName(String theAttachmentFileName) { attachmentFileName = theAttachmentFileName; } public void setSubject(String theSubject) { subject = theSubject; } public void setMsgText(String theMsgText) { msgText1 = theMsgText; } public String[] getMailTo() { return mailTo; } public String getMailFrom() { return mailFrom; } public String getHost() { return host; } public String getAttachmentFileName() { return attachmentFileName; } public String getSubject() { return subject; } public String getMsgText() { return msgText1; } public static void main(String[] args) throws Exception { String To[] = { "abc@123.com", "123@abc.net" }; MailLog oMailer = new MailLog( To, "abc@123.com", "10.1.1.122", "test.txt", "Subject:Testing Mail System.out.println", "Body: Testing MailLog", true); System.out.println("Sending a mail"); oMailer.sendMail(); System.out.println("Sent a mail attachment"); } public void sendMail() throws Exception { // create some properties and get the default Session Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); session.setDebug(debugFlag); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mailFrom)); InternetAddress[] address = new InternetAddress[mailTo.length]; for (int count = 0; count < mailTo.length; count++) { address[count] = new InternetAddress(mailTo[count]); } msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(attachmentFileName); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); // send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } throw new Exception(mex.getMessage()); } //end } /** * Method to send mails without attachment */ public void sendMailWithoutAttachment() throws Exception { // create some properties and get the default Session System.out.println("Entering : MailLog : sendMailWithoutAttachment"); Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); session.setDebug(debugFlag); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mailFrom)); InternetAddress[] address = new InternetAddress[mailTo.length]; for (int count = 0; count < mailTo.length; count++) { address[count] = new InternetAddress(mailTo[count]); } msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // create the Multipart and its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); // send the message Transport.send(msg); } catch (MessagingException mex) { System.out.println( "Exception in sendMailWithoutAttachement method" + mex.getMessage()); mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } throw new Exception(mex.getMessage()); } //end System.out.println("Leaving : MailLog : sendMailWithoutAttachment"); } //end of method sendMailWithoutAttachment() }