Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Is there any limit on the maximum length of filename while sending attachments?

Does it support more than 255 characters?
MIME spec says about encoded-word as:
An encoded-word may not be more than 75 characters long, including charset, encoding, encoded text, and delimiters. If it is desirable to encode more text than will fit in an encoded-word of 75 characters, multiple encoded-words (separated by CRLF SPACE) may be used.
But can we pass the encoded filename as shown below to avoid the extra new line, spaces generated by encodeText() API?
"=?utf-8?B or Q?long_encodedtext?=" [Instead of breaking it into multiple lines]
Eg:
filename="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5zDncOew5/DoMOhw6LDo8Okw6XDpsOnw6jDqcOqw6vDrMOtw67Dr8Oww7HDssOzw7TDtcO2w7jDucO6w7vDvMO9w77Dv8OAw4HDhcOGw4cuZG9j?=" [It works and the base64 is generated by custom code]
The following does not work and generated by MimeUtility.encodeText() API.
Content-Disposition: attachment; filename="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDiQ==?=\
\
=?utf-8?B?w4rDi8OMw43DjsOPw5DDgMOBw4XDhsOH?=\
\
=?utf-8?B?w4jDicOKw4vDjMONw47Dj8OQw5HDksOT?=\
\
=?utf-8?B?w5TDlcOWw5jDmcOaw5vDnMOdw57Dn8Og?=\
\
=?utf-8?B?w6HDosOjw6TDpcOmw6fDqMOpw6rDq8Os?=\
\
=?utf-8?B?w63DrsOvw7DDscOyw7PDtMO1w7bDuMO5?=\
\
=?utf-8?B?w7rDu8O8w73DvsO/w4DDgcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkC5kb2M=?="
Answers
-
Let me see if I understand...
You say that the correctly encoded filename "doesn't work". In what sense does it not work? Exactly what fails and how does it fail?
Are you asking to violate the MIME spec and use more than 75 characters per encoded word? No, there's no way to force JavaMail to do that. Are you doing that because your message is being processed by some program that doesn't implement the MIME spec correctly? Maybe that other program is the thing that needs to be fixed?
-
Thanks Bill for the comments. The issue is if the lengthy encoded word (>75 characters) is passed without including the CRLF spaces then it works fine in Outlook and Gmail but it does not work by sending the lengthy filename using MimeUtility.encodeText() API.
Eg:
//The below code does not work to send the attachment and the attachment name is displaying the UTF-8 characters
String fileName="ÀÁÅÆÀÁÅÆÇÈÉÊËÌÍÎÏÐÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÅÆÇ.doc";
messageBodyPart.setFileName(MimeUtility.encodeText(fileName, "utf-8",null));
//The headers are displayed in Gmail as:
Content-Type: text/plain; name="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOF?="
=?utf-8?B?w4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5w=?=\
\
=?utf-8?B?w53DnsOfw6DDocOiw6PDpMOlw6bDp8Oow6nDqsOrw6zDrcOuw6/DsMOx?=\
\
=?utf-8?B?w7LDs8O0w7XDtsO4w7nDusO7w7zDvcO+w7/DgMOBw4XDhsOHLmRvYw==?="; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOF?=\
\
=?utf-8?B?w4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5w=?=\
\
=?utf-8?B?w53DnsOfw6DDocOiw6PDpMOlw6bDp8Oow6nDqsOrw6zDrcOuw6/DsMOx?=\
\
=?utf-8?B?w7LDs8O0w7XDtsO4w7nDusO7w7zDvcO+w7/DgMOBw4XDhsOHLmRvYw==?="
==============================================
//The below code works and attachment name shows correctly: [Custom generated Base64 code for filename]
String fileName="ÀÁÅÆÀÁÅÆÇÈÉÊËÌÍÎÏÐÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÅÆÇ.doc";
String encodedName = "=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5zDncOew5/DoMOhw6LDo8Okw6XDpsOnw6jDqcOqw6vDrMOtw67Dr8Oww7HDssOzw7TDtcO2w7jDucO6w7vDvMO9w77Dv8OAw4HDhcOGw4cuZG9j?=";
messageBodyPart.setFileName(encodedName);
//The message header in Gmail:
Content-Type: text/plain; name="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5zDncOew5/DoMOhw6LDo8Okw6XDpsOnw6jDqcOqw6vDrMOtw67Dr8Oww7HDssOzw7TDtcO2w7jDucO6w7vDvMO9w77Dv8OAw4HDhcOGw4cuZG9j?="; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5zDncOew5/DoMOhw6LDo8Okw6XDpsOnw6jDqcOqw6vDrMOtw67Dr8Oww7HDssOzw7TDtcO2w7jDucO6w7vDvMO9w77Dv8OAw4HDhcOGw4cuZG9j?="
Query - Is it OK to use the above working code?
-
Ok, I see the problem.
You shouldn't be encoding the filename yourself using MimeUtility.encodeText because JavaMail will detect it as a parameter value that includes CRLF and will add escapes. You should just call messageBodyPart.setFileName(fileName).
That will let JavaMail encode the filename itself, using RFC 2231 (assuming you're using the current version of JavaMail). Or, if the mailers you're using don't support that encoding, you should configure JavaMail to use the non-standard MIME encoding for filenames by setting the System properties "mail.mime.encodefilename" to "true" and "mail.mime.encodeparameters" to "false".