E-mail attachment name is garbled

phenomenon

I noticed that the name of the attachment file of the email sent from the application developed with Spring Boot 2.3.4 (+ spring-boot-starter-mail) may be garbled. image.png This is a view of attachments in emails received in Outlook. Below is the simplified email sending code.

import org.springframework.mail.javamail.JavaMailSender
import org.springframework.mail.javamail.MimeMessageHelper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class SampleMailApplication : CommandLineRunner {

  @Autowired
  lateinit var mailSender: JavaMailSender

  override fun run(vararg args: String?)
    val message = mailSender.createMimeMessage()
    val helper = MimeMessageHelper(message, true)
    helper.setTo(username)
    helper.setFrom(username)
    helper.setSubject("Send")
    helper.setText("test")

    helper.addAttachment("Aiue Okakikukeko", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("Aiue Okakikuke", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("Aiue Okakikuke Kosashi", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("Aiue Okakikuke Kosashisu", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("Aiue Okakikuke Kosashi", ByteArrayResource("AIUEO".toByteArray()), "plain/text")

    helper.addAttachment("1234567890", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("12345678901234567890", ByteArrayResource("AIUEO".toByteArray()), "plain/text")
    helper.addAttachment("123456789012345678901234567890", ByteArrayResource("AIUEO".toByteArray()), "plain/text")

    mailSender.send(message)
  }
}

fun main(args: Array<String>) {
  runApplication<SampleMailApplication>(*args)
}

If the file name has 13 or more double-byte characters, it will be displayed as garbled or encoded.

Email confirmation

If you check the Content-disposition in the email source

//Aiue Okakikuke Kosashi
Content-disposition: attachment;
	filename*=UTF-8''%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A%E3%81%8B%E3%81%8D%E3%81%8F%E3%81%91%E3%81%93

//Aiue Okakikuke Kosashisu
Content-disposition: attachment;
	filename="=UTF-8B44GC44GE44GG44GI44GK44GL44GN44GP44GR44GT44GV44GX44GZ44Gb="

The format of the encoded character string has changed, such as the attribute names being filename * and filename, with 12 and 13 double-byte characters as the boundary. Also, when I sent the same attachment in Outlook, the file name was not garbled, and when I checked the source, the filename * pattern was used even for file names with 13 or more full-width characters.

Source code investigation

When I looked it up there, I found the following code in MimeMessageHelper.

public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
  Assert.notNull(attachmentFilename, "Attachment filename must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  try {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
    mimeBodyPart.setFileName(isEncodeFilenames() ?
      MimeUtility.encodeText(attachmentFilename) : attachmentFilename);

The process does not branch depending on the number of characters, but there may be something because the flag controls whether MimeUtility.encodeText is used. Therefore, if you add the following code before calling helper.addAttachment, the file name will not be garbled even if it is 13 full-width characters or more.

helper.isEncodeFilenames = false

I couldn't dig deep into the root cause, but I'm glad I solved it for the time being.

Recommended Posts

E-mail attachment name is garbled
[Windows] Java code is garbled
When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧