Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail and non-ASCII character in filenames

Tags:

jakarta-mail

I can send attachments that have non-ascii filenames in JavaMail but I am not able to download them. I am getting java.io.FileNotFoundException specifically for those attachments whose file names contain non-ascii characters.

FYI: I am using something like messageBodyPart.setFileName(MimeUtility.encodeText(filename[i])) to encode the text and MimeUtility.decodeText(bodyPart.getFileName()) to decode the non-ascii file names

Is there a workaround for this?

EDIT @Bill, here is part of my code that reads attachments. I have also added the properties.setProperty("mail.mime.decodeparameters", "true") and properties.setProperty("mail.mime.decodefilename", "true") properties in my code.

if (message[a].getContent() instanceof MimeMultipart) {
                Multipart multipart = (Multipart) message[a].getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    bodyPart = multipart.getBodyPart(i);                     
                    disposition = bodyPart.getDisposition();                    
                    if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT) || (disposition.equals(BodyPart.INLINE)))) {
                        DataHandler handler = bodyPart.getDataHandler(); 
                        String path = bodyPart.getFileName();
                        String[] str = path.split("/");                         
                        String fileName = str[str.length - 1];                       

                        String filePath = ReadConfigPropertiesFile.getPropertyValue("server.buildpath");
                        System.out.println(fileName);
                        File tempDir = new File(filePath + user);
                        if (!tempDir.exists()) {
                            tempDir.mkdir();
                        }
                        File saveFile = new File(tempDir + "/" + fileName);
                        int count = 0;
                        while (saveFile.exists()) {
                          count++;
                          saveFile = new File(tempDir + "/" + count + "_" + fileName);
                        } 

                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile));
                        byte[] buff = new byte[2048];
                        InputStream is = bodyPart.getInputStream();
                        int ret = 0;
                        while ((ret = is.read(buff)) > 0) {
                            bos.write(buff, 0, ret);
                        }
                        bos.close();
                        is.close();
                        //System.out.println(bodyPart.getContentType());


                    }else {
                        //display body (message) of the attachment;
                        //System.out.println(bodyPart.getContent().toString());

                   }
           }
     }

The above code raises the FileNotFoundException exception at BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile)) line and this is getting raised for the attachments whose file names are non-ascii characters (something like ሰላም.pdf). Every thing else works fine.

like image 285
Semytech Avatar asked Mar 09 '13 12:03

Semytech


1 Answers

This answer taken from comment of @semytech (OP). It was hard to find it there, so I will add it as answer for more visibility. It helped me with hebrew filenames.

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(MimeUtility.encodeText(filename, "UTF-8", null));
like image 191
yurin Avatar answered Oct 21 '22 17:10

yurin