1 Reply Latest reply: Mar 30, 2012 2:00 PM by Bill Shannon-Oracle RSS

    Problem to get file with special characters in the message

    927554
      Hi, I'm developing an application that read the email and save the attached file. However, some files have special characters in the name, like: Documento de EspecificaÇÂO.doc

      I noticed the de name of the file in de message head is:
      "=?iso-8859-1?Q?Documento_de_Especifica=E7=E3o.doc?="

      I'm already using the JavaMil 1.4.5

      Tha is my code:

      package br.com.cesan.helpdesk;

      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.util.Properties;

      import javax.mail.Flags;
      import javax.mail.Folder;
      import javax.mail.Message;
      import javax.mail.MessagingException;
      import javax.mail.Multipart;
      import javax.mail.Part;
      import javax.mail.Session;
      import javax.mail.Store;
      import javax.mail.search.FlagTerm;

      public class LerEmail2 {

           /**
           * @param args
           * @throws MessagingException
           * @throws IOException
           */
           public static void main(String[] args) throws MessagingException, IOException {
                // TODO Auto-generated method stub
                
                // Get session
           Session session = Session.getInstance(new Properties(), null);

           // Get the store
           Store store = session.getStore("pop3");
           store.connect("pop.xxxxx.com.br", "user", "password");
           
           Folder folder = store.getFolder("INBOX");
                //folder.open(Folder.READ_ONLY);
                folder.open(Folder.READ_WRITE);
                
                // Show only unreaded Messages
                FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);

                
                // Get directory
           Message message[] = folder.getMessages();     
           //Message messages[] = folder.search(ft);

           
           for (int i=0, n=message.length; i<n; i++) {
                
                System.out.println(i + " - "+ message.getSubject() +" - " + message[i].getSentDate() );
                
                Object content = message[i].getContent();

                if (content instanceof Multipart) {
           handleMultipart((Multipart)content);
           } else {
           handlePart(message[i]);
           }

                
           }
           
           
           // Close connection
           folder.close(false);
           store.close();
                
           }
           
           public static void handleMultipart(Multipart multipart) throws MessagingException, IOException {

                for (int i=0, n=multipart.getCount(); i<n; i++) {
                     handlePart(multipart.getBodyPart(i));
                }
           }

           
           public static void handlePart(Part part) throws MessagingException, IOException {
           
                String disposition = part.getDisposition();
           String contentType = part.getContentType();
           
           if (disposition == null) { // When just body
           
                System.out.println("Null: " + contentType);
           
                // Check if plain
                if ((contentType.length() >= 10) && (contentType.toLowerCase().substring(0, 10).equals("text/plain"))) {
                     part.writeTo(System.out);
                
                } else { // Don't think this will happen
                     System.out.println("Other body: " + contentType);
                     part.writeTo(System.out);
                }
           
           } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
           
                System.out.println("Attachment: " + part.getFileName() + " : " + contentType);
                saveFile(part.getFileName(), part.getInputStream());
           
           } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
           System.out.println("Inline: " +
           part.getFileName() +
           " : " + contentType);
           saveFile(part.getFileName(), part.getInputStream());
           } else {  // Should never happen
           System.out.println("Other: " + disposition);
           }
           
           }

           
           public static void saveFile(String filename, InputStream input) throws IOException {
                     
                if (filename == null) {
                     filename = File.createTempFile("xx", ".out").getName();
                }
                
                // Do no overwrite existing file
                File file = new File(filename);

                for (int i=0; file.exists(); i++) {
                     file = new File(filename+i);
                }
                
                System.setProperty("file.encoding", "iso-8859-1");
                
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                BufferedInputStream bis = new BufferedInputStream(input);
                int aByte;
                while ((aByte = bis.read()) != -1) {
                     bos.write(aByte);
                }
                
                bos.flush();
                bos.close();
                bis.close();
           }

      }

      The problem occurs in:
      FileOutputStream fos = new FileOutputStream(file);
      BufferedOutputStream bos = new BufferedOutputStream(fos);

      BufferedInputStream bis = new BufferedInputStream(input);
      int aByte;
      while ((aByte = bis.read()) != -1) {
      bos.write(aByte);
      }

      Thanks

      Edited by: user10283976 on 30/03/2012 07:36

      Edited by: user10283976 on 30/03/2012 07:37

      Edited by: user10283976 on 30/03/2012 07:37

      Edited by: user10283976 on 30/03/2012 07:38

      Edited by: user10283976 on 30/03/2012 07:40

      Edited by: user10283976 on 30/03/2012 07:41

      Edited by: user10283976 on 30/03/2012 07:42