System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
客户邮箱用的是outlook邮箱
原因:附件名过长
参数mail.mime.splitlongparameters 在linux下 会默认为 true,附件名过长,就会被截断
helper.addAttachment(MimeUtility.encodeWord(fileName,"utf-8","B"), source);
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(MimeUtility.encodeText(attachmentFilename));
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
getRootMimeMultipart().addBodyPart(mimeBodyPart);
}
catch (UnsupportedEncodingException ex) {
throw new MessagingException("Failed to encode attachment filename", ex);
}
}
public void setFileName(String filename) throws MessagingException {
setFileName(this, filename);
}
static void setFileName(MimePart part, String name) throws MessagingException {
...
ParameterList p = cd.getParameterList();
...
if (value.length() > 60 && splitLongParameters && encodeParameters) {
private static final boolean splitLongParameters = PropUtil.getBooleanSystemProperty("mail.mime.splitlongparameters", true);
这次遇到的是在方法中已经设置的false在生产环境中依然有问题,最后发现是其他方法有发送附件且没有设置为false,并且这个环境变量是只能配置一次。
所以最好是放到启动类,我的代码是写在Listener中的,我就直接放到了static代码块中