Spring MimeMessageHelper attachment filename encoding

后端 未结 2 791
走了就别回头了
走了就别回头了 2021-01-07 11:05

I am sending mail with MimeMessageHelper in my Spring Boot application.

How can I tell it to encode the filename, which contains the letter

2条回答
  •  礼貌的吻别
    2021-01-07 11:32

    I've solved the the problem with these lines:

    • System.setProperty("mail.mime.splitlongparameters", "false")
    • MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8")
    • MimeUtility.encodeWord(attachmentFilename)

    Here is the example code,

    System.setProperty("mail.mime.splitlongparameters", "false");
    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    
    // Your email content
    helper.setFrom("...");
    helper.setTo("...");
    helper.setSubject("...");
    helper.setText("...");
    
    helper.addAttachment(
        MimeUtility.encodeWord(attachmentFilename),
        attachmentContent
    );
    

提交回复
热议问题