问题描述: java中发送email,附件excel里的内容为乱码
解决方法:
Multipart multipart = new MimeMultipart();
MimeBodyPart contentPart = new MimeBodyPart();
contentPart.setText(content,"UTF-8");
contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
multipart.addBodyPart(contentPart); /*添加附件*/
MimeBodyPart fileBody = new MimeBodyPart();
//以下屏蔽掉的这行代码,是错误的,会导致附件excel表格里面的内容乱码
// DataSource source = new ByteArrayDataSource(attachments.get(0), "application/msexcel");
FileDataSource source=new FileDataSource(attachments.get(0));
fileBody.setDataHandler(new DataHandler(source));
fileBody.setFileName(MimeUtility.encodeText(excelName));
multipart.addBodyPart(fileBody);
mailMessage.setContent(multipart);
mailMessage.saveChanges();