Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download attachments using Java Mail

Now that I`ve downloaded all the messages, and store them to

Message[] temp; 

How do I get the list of attachments for each of those messages to

List<File> attachments; 

Note: no thirdparty libs, please, just JavaMail.

like image 479
George Avatar asked Nov 17 '09 11:11

George


People also ask

What is MimeMessage in Java?

MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message. The mail. mime. address. strict session property controls the parsing of address headers.

What is JavaMail API?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.

How can I send multiple attachments by email in Java?

setText(body); multipart. addBodyPart(msgBodyPart); msgBodyPart = new MimeBodyPart(); //attach file DataSource source = new FileDataSource(attachFile); messageBodyPart. setDataHandler(new DataHandler(source)); messageBodyPart. setFileName(attachFile); multipart.


2 Answers

Without exception handling, but here goes:

List<File> attachments = new ArrayList<File>(); for (Message message : temp) {     Multipart multipart = (Multipart) message.getContent();      for (int i = 0; i < multipart.getCount(); i++) {         BodyPart bodyPart = multipart.getBodyPart(i);         if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&                StringUtils.isBlank(bodyPart.getFileName())) {             continue; // dealing with attachments only         }          InputStream is = bodyPart.getInputStream();         // -- EDIT -- SECURITY ISSUE --         // do not do this in production code -- a malicious email can easily contain this filename: "../etc/passwd", or any other path: They can overwrite _ANY_ file on the system that this code has write access to! //      File f = new File("/tmp/" + bodyPart.getFileName());         FileOutputStream fos = new FileOutputStream(f);         byte[] buf = new byte[4096];         int bytesRead;         while((bytesRead = is.read(buf))!=-1) {             fos.write(buf, 0, bytesRead);         }         fos.close();         attachments.add(f);     } } 
like image 82
David Rabinowitz Avatar answered Oct 21 '22 21:10

David Rabinowitz


Question is very old, but maybe it will help someone. I would like to expand David Rabinowitz`s answer.

if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) 

should not return all atachments as you expect, because you can have mail where mixed part is without defined disposition.

   ----boundary_328630_1e15ac03-e817-4763-af99-d4b23cfdb600 Content-Type: application/octet-stream;     name="00000000009661222736_236225959_20130731-7.txt" Content-Transfer-Encoding: base64 

so in this case, you can also check for filename. Like this:

if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) && StringUtils.isBlank(part.getFileName())) {...} 

EDIT

there is whole working code using condition descibed above.. Because each part can encapsulate another parts and attachment should be nested in, recursion is used to traverse through all parts

public List<InputStream> getAttachments(Message message) throws Exception {     Object content = message.getContent();     if (content instanceof String)         return null;              if (content instanceof Multipart) {         Multipart multipart = (Multipart) content;         List<InputStream> result = new ArrayList<InputStream>();          for (int i = 0; i < multipart.getCount(); i++) {             result.addAll(getAttachments(multipart.getBodyPart(i)));         }         return result;      }     return null; }  private List<InputStream> getAttachments(BodyPart part) throws Exception {     List<InputStream> result = new ArrayList<InputStream>();     Object content = part.getContent();     if (content instanceof InputStream || content instanceof String) {         if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {             result.add(part.getInputStream());             return result;         } else {             return new ArrayList<InputStream>();         }     }      if (content instanceof Multipart) {             Multipart multipart = (Multipart) content;             for (int i = 0; i < multipart.getCount(); i++) {                 BodyPart bodyPart = multipart.getBodyPart(i);                 result.addAll(getAttachments(bodyPart));             }     }     return result; } 
like image 22
mefi Avatar answered Oct 21 '22 20:10

mefi