[JAVA] The attached file name was garbled in the Spring Boot email, so take measures

E-mail attachments are garbled

org.springframework.boot:spring-boot-starter-mail

When I sent the attached file using Spring's mail sending method, which is available on the Web and Qiita, the attached file name was garbled. Screen Shot 2019-04-01 at 08.00.21.png

Apparently, it is OK if the file name is only in Japanese, but it seems that this garbled character occurs when the file identifier is entered (I have not investigated in detail here)


    private void sendMail(File file) throws MessagingException, URISyntaxException, IOException {
        JavaMailSender sender = this.getMailSender();
        
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.name());
        
        String toAddr = this.mailToAddr;
        helper.setTo(toAddr);

        //Supports multiple CC addresses (separated by commas))
        if (!StringUtils.isEmpty(this.mailCcAddrs)) {
            String[] ccAddrArr = this.mailCcAddrs.split(",");
            helper.setTo(ccAddrArr);
        }
        String subject = "This is a test email"; 
        helper.setSubject(subject);
        
        //Email body template acquisition
        Path path = Paths.get(getClass().getClassLoader()
                .getResource("mail_template.txt").toURI());
                    
        Stream<String> lines = Files.lines(path);
        String template = lines.collect(Collectors.joining("\n"));
        String body = template.replace("[[Replace]]", toAddr);
        lines.close();
        helper.setText(body);
        
        //attaching file
        helper.addAttachment("The name of the attached file The attachment is a template.txt", file);
        
        sender.send(message);
    }

Fixed the following points

Process the file name of the attached file once

MimeUtility.encodeWord("file name");

Screen Shot 2019-04-01 at 07.59.36.png

Add mail settings to system properties

Change this property before creating your first MimeMessage instance

System.setProperty("mail.mime.splitlongparameters", "false");

Screen Shot 2019-04-01 at 07.59.20.png

Check the file name after the above correction

Screen Shot 2019-04-01 at 08.00.06.png

Recommended Posts

The attached file name was garbled in the Spring Boot email, so take measures
Email sent by JavaMail Gmail cannot restore the file name of the attached file
Static file access priority in Spring boot
Local file download memorandum in Spring Boot
How to set environment variables in the properties file of Spring boot application
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
View the Gradle task in the Spring Boot project
Specify the encoding of static resources in Spring Boot
I want to control the maximum file size in file upload for each URL in Spring Boot
Spring Boot 1.x will reach EOL in the next year.
Put the file in the properties of string in spring xml configuration
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot