当我使用JavaMailSender发送带有附件的电子邮件时,它总是失败并抛出以下异常:
org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Exception writing Multipart
; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Exception writing Multipart
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1365)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:462)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:359)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:354)
这是我的代码
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(userName);
helper.setTo(toAddress);
helper.setSubject(subject);
FileSystemResource file = new FileSystemResource(filePath);
helper.addAttachment(file.getFilename(), file);
} catch (Exception e) {
log.error("oops..., ", e);
}
mailSender.send(message);
答案 0 :(得分:0)
em,我已经运气解决了这个问题。
只需设置带有附件的空白文本内容即可,
helper.addAttachment(MimeUtility.encodeText("")), new ByteArrayResource(IOUtils.toByteArray(inputStream)));
helper.setText("", true);
答案 1 :(得分:0)
设置内容类型,我通过将内容类型设置为第三个参数来解决了这个问题
helper.addAttachment(“附件文件名”,新的ByteArrayResource(IOUtils.toByteArray(inputStream)),“ application / pdf” );