1 回答
TA贡献1786条经验 获得超11个赞
我知道了 !我查看了 MimeMessage 文档并找到了解决方案,您只需将 try(在 SendMail.java 中)的内容更改为:
MimeMessage mimeMessage = new MimeMessage(session);
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/plain; charset=UTF-8");
mimeMultipart.addBodyPart(messageBodyPart);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
String filename = "path to your file, exemple : /storage/path.txt" ;
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(filename);
mimeMultipart.addBodyPart(attachmentBodyPart);
mimeMessage.setFrom(new InternetAddress(Config.MAIL_SENDER));
mimeMessage.addRecipient(Message.RecipientType.TO, new
InternetAddress(Config.MAIL_RECEIVER));
mimeMessage.setSubject(subject);
mimeMessage.setContent(mimeMultipart);
Transport.send(mimeMessage);
我还更改了我使用的实例的名称。
添加回答
举报