java - 使用 java mail api 触发邮件时在邮件中添加了不必要的附件
问题描述
我正在使用 java mailer 发送带有内联图像和 html 文件作为附件的电子邮件,但今天我注意到在邮件“ATT00001.bin”中发送了不必要的附件,我没有在代码中的任何地方添加,
真的对我来说太震惊了,所以我通过更改附件名称尝试了我的水平,但我找不到解决方案(直到昨天它工作正常(我的意思是我曾经在邮件中获得一个内联图像和一个附件)
[![// Recipient's email ID needs to be mentioned.
String to = "mail@example.com";
// Sender's email ID needs to be mentioned
String from = "sender@example.com";
final String username = "user";//change accordingly
final String password = "pwd";//change accordingly
// Assuming you are sending email through outlook mail
String host = "outlook.office365.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
//message.setSubject("Testing Subject");
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<p>Hi Team,</p> <p>Please find the Report for the day </p> <p> </p> <img src=\"cid:image\"> <p> Regards,</p> <p> Team</p>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
//local file path
DataSource fds = new FileDataSource("$localDirectory");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
//adding attachment as well
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "$reportFileName";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("API_SummaryReport_${fileNamePart}+.html");
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);
log.info("Email sent successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}][1]][1]
只需要一个附件,但要获取 bin 格式的其他文件,请查找图片以供参考
解决方案
请将代码修改为:
String to = "mail@example.com";
// Sender's email ID needs to be mentioned
String from = "sender@example.com";
final String username = "user";
final String password = "pwd";
String host = "outlook.office365.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(msgSubject);
message.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<p>Hi Team,</p> <p>Please find the Report for the day </p> <p> </p> <img src=\"cid:image\"> <p> Regards, </p> <p> Team</p>";
messageBodyPart.setContent(htmlText, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<image>");
//add this to avoid unwanted attachment.
imagePart.setDisposition(MimeBodyPart.INLINE);
imagePart.attachFile(new File("C:\\abc.png"));
multipart.addBodyPart(imagePart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException | IOException ex) {
throw new RuntimeException(e);
}