2 votes

Pièce jointe non nécessaire ajoutée dans le courrier lors du déclenchement du courrier à l'aide de l'api java mail

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>&nbsp;</p> <img src=\"cid:image\">  <p>&nbsp;Regards,</p> <p>&nbsp;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.

1voto

Anish B. Points 589

Veuillez modifier le code comme suit :

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>&nbsp;</p> <img src=\"cid:image\">  <p>&nbsp;Regards,  </p> <p>&nbsp;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);
    }

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X