Forum Stats

  • 3,854,950 Users
  • 2,264,439 Discussions
  • 7,905,846 Comments

Discussions

Is there any limit on the maximum length of filename while sending attachments?

user6731020
user6731020 Member Posts: 2
edited Aug 10, 2015 3:34PM in JavaMail

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

  • Bill Shannon-Oracle
    Bill Shannon-Oracle Member Posts: 5,441 Bronze Badge
    edited Jul 31, 2015 2:49PM

    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?

  • user6731020
    user6731020 Member Posts: 2
    edited Aug 7, 2015 7:54AM

    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?

  • Bill Shannon-Oracle
    Bill Shannon-Oracle Member Posts: 5,441 Bronze Badge
    edited Aug 10, 2015 3:34PM

    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".

This discussion has been closed.