Failed to load PDF attachment using java using JavaMail

I am creating a mail client using the JavaMail API. Everything works fine, since I can connect to the mail server (using IMAP), delete mail, receive received letters and show them to the user, etc.

Now the problem is when it comes to downloading PDF attachments. PDF files do not load completely ... some files are missing.

If any PDF attachment is 38 KB in size when downloading the attachment using IE or any other web browser, but when I download it using my java code, it has a size of 37.3 Kb. It is not complete. Therefore, when I try to open it using Adobe Reader, the error message "File is corrupted ..."

Here is the code I wrote to download the attachment:

public boolean saveFile(String filename,Part part) throws IOException, MessagingException { boolean ren = true; FileOutputStream fos = null; BufferedInputStream fin = null; InputStream input = part.getInputStream(); File pdffile = new File("d:/"+filename); try{ if(!pdffile.exists()){ fos = new FileOutputStream(pdffile); fin = new BufferedInputStream(input); int size = 512; byte[] buf = new byte[size]; int len; while ( (len = fin.read(buf)) != -1 ) { fos.write(buf, 0, len); } input.close(); fos.close(); }else{ System.out.println("File already exists"); } }catch(Exception e ){ ren = false; } return ren; } 

Am I missing something? Any helpful help is appreciated.

+4
source share
2 answers

Finally, I found a solution in JavaMail Frequently Asked Questions Reading Mail, IMAP Section Gmail Server Works with Attachment Errors

At first I tried to set the partialfetch property to false, but sometimes it works sometimes it doesn't

  props.setProperty("mail.imap.partialfetch", "false"); 

There is another way mentioned in the FAQ that simply uses the MimeMessage copy constructor and stores the orignal object in some tempmsg and then gets the contents of tempmsg

  MimeMessage tempmsg = new MimeMessage(msg); Multipart part = (Multipart) tempmsg.getContent(); 

and now carry out all the operations in which he must work.

For detailed information on what is actually happening, goto JavaMail FAQs In the Read Mail section of IMAP, you will find all the answers.

+1
source

Spent a few hours on it and finally figured it out.

 props.setProperty("mail.imaps.partialfetch", "false"); 

did it for me. Almost the same as @Shantanu above, but because I used

 store = session.getStore("imaps"); 

I needed to use "imap s " for partial sampling.

It works like a charm.

Full code below:

 // Load mail properties Properties mailProperties = System.getProperties(); mailProperties.put("mail.mime.base64.ignoreerrors", "true"); mailProperties.put("mail.imaps.partialfetch", "false"); // Connect to Gmail Session session = Session.getInstance(mailProperties, null); store = session.getStore("imaps"); store.connect("imap.gmail.com", -1, "username", "password"); // Access label folder Folder defaultFolder = store.getDefaultFolder(); Folder labelFolder = defaultFolder.getFolder("mylabel"); labelFolder.open(Folder.READ_WRITE); Message[] messages = labelFolder.getMessages(); saveAttachments(messages); 

...

  private void saveAttachments(Message[] messages) throws Exception { for (Message msg : messages) { if (msg.getContent() instanceof Multipart) { Multipart multipart = (Multipart) msg.getContent(); for (int i = 0; i < multipart.getCount(); i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equalsIgnoreCase(Part.ATTACHMENT) || (disposition.equalsIgnoreCase(Part.INLINE))))) { MimeBodyPart mimeBodyPart = (MimeBodyPart) part; String fileName = mimeBodyPart.getFileName(); File fileToSave = new File(fileName); mimeBodyPart.saveFile(fileToSave); } } } } } 
+4
source

Source: https://habr.com/ru/post/1391815/


All Articles