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 文件它到达完全空白(但具有正确的页数)。此外,到达的文件的大小大于原始文件(例如 100 kb 对应 70 kb)。你对此有什么想法吗?
先感谢您