java - 使用 java mail api 触发邮件时在邮件中添加了不必要的附件

标签 java email groovy

我正在使用 java 邮件程序发送带有内联图像和 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>&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]

希望只有一个附件,但会得到其他 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>&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);
    }

关于java - 使用 java mail api 触发邮件时在邮件中添加了不必要的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55390869/

相关文章:

JavaFX 将事件监听器附加到黑色圆圈

grails - 如何避免groovy/XMLSlurper从节点剥离html标签?

java - 使用java和xsl将xml转换为csv时如何处理重复的节点名称

java - Spring 4 属性文件不起作用

java - 有没有办法使for循环中的循环增加随机化?

excel - 在 Outlook 2013 中安排在每周开始时发送一封电子邮件

java - 带参数的 @Immutable 类的构造函数无法从 .java 类访问

java - 在 Java/印度编号系统中格式化数字

php - 在数据库中搜索重复项时,我是否应该将电子邮件区分大小写?

Python 电子邮件正文为空