Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How Read attachment form eml file ?

843834Aug 13 2009 — edited Aug 13 2009
i try to get details form .eml(e-mail) file, which contain attachment.it works if that attachment types are pdf/word..
but if that attachment type .eml(means email file have email attachment) then it is not work returning null.Can you help to extract that attachment.
This is the code...
package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;

public class ReadMultipartMail {

	public static void main(String args[]) throws Exception {
		 File emlFile = new
		 File("F:/test.eml");

		InputStream source;

		source = new FileInputStream(emlFile);

		Properties props = System.getProperties();
		props.put("mail.host", "smtp.dummydomain.com");
		props.put("mail.transport.protocol", "smtp");

		Session mailSession = Session.getDefaultInstance(props, null);
		MimeMessage message = new MimeMessage(mailSession, source);
		System.out.println("Subject : " + message.getSubject());
		System.out.println("From : " + message.getFrom()[0]);

		String from = InternetAddress.toString(message.getFrom());
		if (from != null) {
			System.out.println("From: " + from);
		}

		String replyTo = InternetAddress.toString(message.getReplyTo());
		if (replyTo != null) {
			System.out.println("Reply-to: " + replyTo);
		}
		String to = InternetAddress.toString(message
				.getRecipients(Message.RecipientType.TO));
		if (to != null) {
			System.out.println("To: " + to);
		}

		String subject = message.getSubject();
		if (subject != null) {
			System.out.println("Subject: " + subject);
		}
		Date sent = message.getSentDate();
		if (sent != null) {
			System.out.println("Sent: " + sent);
		}

		System.out.println();
		System.out.println("Message : ");

		Multipart multipart = (Multipart) message.getContent();

		for (int x = 0; x < multipart.getCount(); x++) {
			BodyPart bodyPart = multipart.getBodyPart(x);

			String disposition = bodyPart.getDisposition();

			if (disposition != null
					&& (disposition.equals(BodyPart.ATTACHMENT))) {
				System.out.println("Mail have some attachment : ");

				DataHandler handler = bodyPart.getDataHandler();
				System.out.println("file name : " + handler.getName());
			} else {
				System.out.println(bodyPart.getContent());
			}
		}
		System.out.println();
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 10 2009
Added on Aug 13 2009
1 comment
1,898 views