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.
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);
}
MimeUtility.encodeWord("file name");
Change this property before creating your first MimeMessage instance
System.setProperty("mail.mime.splitlongparameters", "false");
Recommended Posts