J'envoie l'email avec une image en ligne et un fichier html en pièce jointe en utilisant java mailer mais aujourd'hui j'ai remarqué qu'une pièce jointe inutile a été envoyée dans l'email "ATT00001.bin" que je n'ai ajouté nulle part dans le code,
Vraiment, c'est si choquant pour moi que j'ai essayé à mon niveau en changeant le nom de la pièce jointe mais je n'ai pas pu trouver la solution (cela fonctionnait bien jusqu'à hier (je veux dire que j'avais l'habitude de recevoir une image en ligne et une pièce jointe dans le mail).
[![// 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]
Je n'attends qu'une seule pièce jointe mais je reçois un fichier supplémentaire au format bin. Veuillez trouver l'image pour référence.