JavaMail and non ASCII characters in file names
I can send attachments with non ASCII file names in JavaMail, but I can't download them I specifically get Java. Java for attachments whose file names contain non ASCII characters io. FileNotFoundException.
For reference only: I am using a similar messagebodypart Setfilename (mimeutility. Encodetext (filename [i]) to encode text and mimeutility Decodetext (bodypart. Getfilename()) to decode non ASCII file names
Is there a solution?
Edit @ bill, this is part of my code, read the attachment I also added properties to my code Setproperty ("mail. Mime. Decodeparameters", "true") and properties Setproperty ("mail. Mime. Decodefilename", "true") property
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,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 a FileNotFoundException on the line bufferedoutputstream BOS = new bufferedoutputstream (savefile)), and this is raised for attachments with file names of non ASCII characters (similar to ሰ ላ ም. PDF) Everything else is fine
Solution
This answer is taken from the comment of @ semytech (OP) It's hard to find it there, so I'll add it as a more visible answer It helps me use Hebrew file names
MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(MimeUtility.encodeText(filename,"UTF-8",null));