Mark一下
有兩個(gè)地方需要處理
全局的設(shè)置
static {
System.setProperty("mail.mime.splitlongparameters", "false");
System.setProperty("mail.mime.charset", "UTF-8");
}
文件名處理
//編碼處理
String filename = MimeUtility.encodeText(attachment.getName());
//去掉換行和回車
filename = filename.replaceAll("\r", "").replaceAll("\n", "");
具體可以進(jìn)源碼查看
// Set the Content-Disposition "filename" parameter
String s = part.getHeader("Content-Disposition", null);
ContentDisposition cd =
new ContentDisposition(s == null ? Part.ATTACHMENT : s);
// ensure that the filename is encoded if necessary
String charset = MimeUtility.getDefaultMIMECharset();
ParameterList p = cd.getParameterList();
if (p == null) {
p = new ParameterList();
cd.setParameterList(p);
}
if (encodeFileName)
p.setLiteral("filename", name);
else
p.set("filename", name, charset);
part.setHeader("Content-Disposition", cd.toString());