我遇到了javax.mail的问题。我有一个来自Web服务的MultipartFile ArrayList,我必须添加到电子邮件作为附件。来源:
mailMessage.setSubject(主题); mailMessage.setFrom(new InternetAddress(user));
if (attacchments != null && attacchments.size() > 0) {
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(text);
for(MultipartFile attachment : attacchments){
DataSource datasource = new ByteArrayDataSource(attachment.getInputStream(), attachment.getContentType());
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDisposition(Part.ATTACHMENT);
attachmentPart.setHeader("Content-Type", attachment.getContentType());
attachmentPart.setHeader("Content-Transfer-Encoding", "Base64");
attachmentPart.setFileName(MimeUtility.encodeText(attachment.getName() + ".pdf"));
attachmentPart.setDataHandler(new DataHandler(datasource));
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(attachmentPart);
}
mailMessage.setContent(multipart);
mailMessage.saveChanges();
}
Transport transport = mailSession.getTransport();
transport.connect(SMTPServer, 465, user, password);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
我没有例外,电子邮件正确到达内容文本,附件也到了。如果它是一个txt文件,如果它是一个图像它到达已损坏,如果它是一个pdf文件,它到达完全空白(但具有正确的页数)。此外,到达的文件具有大于原始的大小(例如,对于70kb为100kb)。你对此有什么想法吗?
提前谢谢