5

I have an incoming MimeMessage in my JAMES mail server. I want to create an eml file dumping the message completely. I tried using the writeTo method of MimeMessage - resulting file contains only the text body of the email. The attachments are not written to the eml file. My code is something like

String logFileName = "dumpNow.eml";
incomingEmail.getMessage().writeTo(new FileOutputStream(new File(logFileName)));

I do not get any multipart content in the dump. Is there any Util available to do this? Apache Mimeutils is also giving the same result.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • That should work. What version of JavaMail are you using? – Bill Shannon Mar 24 '16 at 20:32
  • There are some issues , if the message originated from Mac machines. I see problem only for those messages. – Kris Jun 01 '16 at 09:13
  • You'll need to provide more detail. What's an example of a message that fails? How does it fail? What version of JavaMail are you using? Is it exactly a MimeMessage or is it a subclass? – Bill Shannon Jun 01 '16 at 19:01

1 Answers1

0

Try this :

// Create your attachement file
File emlFile = new File("myFile.eml");
emlFile.createNewFile();
incomingEmail.getMessage().writeTo(new FileOutputStream(emlFile));

MimeBodyPart attachment = new MimeBodyPart();

DataSource source = new FileDataSource(emlFile);

attachment.setDataHandler(new DataHandler(source));
attachment.setHeader("Content-Type", "application/octet-stream");
attachment.setFileName("myFileName.eml");
attachment.setDescription("My file description");
attachment.setDisposition(Part.ATTACHMENT);

multipart.addBodyPart(attachmentFile);

I think it is because you missed to set the header and the disposition in your code.

Hope it helps,

Beginner
  • 161
  • 4
  • 12
  • I think , this is not relevant to the question :-). I was mentioning about dumping a multipart content to a file (remote to local). Seems you got it reverse. – Kris Nov 28 '17 at 07:56
  • BTW, I had to parse the body tree for emails which had the multipart-mixed contents to do this. – Kris Nov 28 '17 at 07:58