Sending Attachments with the Javamail 1.4.x API

Make your emails interesting with attachments!

Not that your emails aren’t already interesting – if you have some kind of regular job running and you want to produce a results bound file sent to your recipients as an attachment, this code example can illustrate one way it can be done. It’s pretty much the same thing as sending a regular email except that it uses multipart attachments as the body content of the message:

package com.faceroller.mail;

public class Mailer {
	private static final Log log = LogFactory.getLog(Mailer.class);

	public static void send(Email email)
			throws MessagingException, NamingException, IOException {

		 * prefer the jndi lookup in your container, but when debugging
		 * manually setting properties explicitly will do

		// InitialContext ictx = new InitialContext();
		// Session session = (Session) ictx.lookup("java:/Mail");

		Properties props = (Properties) System.getProperties().clone();
		props.put("mail.transport.protocol", "smtp");
		props.put("", host);
		props.put("mail.smtp.port", port);
		props.put("mail.debug", "true");

		 * create the session and message
		Session session = Session.getInstance(props, null);

		 * set the message basics
		MimeMessage message = new MimeMessage(session);
		Message.setFrom(InternetAddress.parse(email.getFrom(), false)[0]);
			InternetAddress.parse(email.getTo(), false)

		 * multipart attachments here, part one is the message text, 
		 * the other is the actual file. notice the explicit mime type 
		 * declarations
		Multipart multiPart = new MimeMultipart();

		MimeBodyPart messageText = new MimeBodyPart();
		messageText.setContent(email.getBodyAsText(), "text/plain");

		MimeBodyPart report = new MimeBodyPart();
		report.setContent(email.getAttachmentAsText(), "text/xml");

		MimeBodyPart rarAttachment = new MimeBodyPart();
		FileDataSource rarFile = new FileDataSource("C:/my-file.rar");
		rarAttachment.setDataHandler(new DataHandler(rarFile));

		 * set the message's content as the multipart obj

		 * do the actual sending here
		Transport transport = session.getTransport("smtp");

		try {

			transport.connect(username, password);
			transport.sendMessage(message, message.getAllRecipients());

			log.warn("Email message sent");

		} finally {

You’ll notice the first multipart’s content is a String with the mime type “text/plain”, this is what will render this part as the message’s body. You can set as many parts as you want, each one defined as a separate attachment. If you want to attach rar or zipped up archive, you can use the activation libraries to include them in as one of the parts. The MimeBodyPart will automatically detect and fill in the mime type for the file – it’s provided by FileDataSource. In JBoss if you’re using the container’s mail service, you can configure the mail server properties in the deploy/mail-service.xml file, and then you can use the initial context to get a handle on that configured mail server.

Get the jars and supporting docs from the Javamail site here:

Comments (1)

  1. 2:50 AM, June 30, 2014anjaneya  / Reply

    Thanks man…it helped me a lotttttttttttttt………………..

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.