2

我想为此在邮件正文中附加一个内联图像我正在使用以下方法一切都很好,但是电子邮件中附加了一个带有“noname”的附件,我不想在电子邮件中添加任何附件我只想在电子邮件中显示一个图像正文,以下是我的代码和邮件的快照

public void forgotPasswordMail(String email,String token)
        {
              String to = email;
              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 {
                  MimeMessage message = new MimeMessage(session);
                  message.setSubject("HTML  mail with images");
                  message.setFrom(new InternetAddress("me@sender.com"));
                  message.addRecipient(Message.RecipientType.TO,
                       new InternetAddress("you@receiver.com"));

                  // Mail Body
                  MimeMultipart multipart = new MimeMultipart("related");
                  BodyPart textPart = new MimeBodyPart();
                  String htmlText ="<img src=\"cid:image\"> " + 
                  "<html><head><style>h1 {background-color: #FFF100;padding: 15px; text-indent: 40px;} " +
                          "p {text-indent: 60px;}</style></head><body><h1>Forgot password request</h1> " +
                          "<p> Please click on the following link to verify your account </p>" + 
                          "<p>" + frontendUrl+"resetForgottonPassword/"+token+"</p></div></body></html>";
                  textPart.setContent(htmlText, "text/html");

                  multipart.addBodyPart(textPart);
                  BodyPart imagePart = new MimeBodyPart();
                  DataSource fds = new FileDataSource
                    ("C:/Users/INTERN I/Desktop/logo2.png");
                  imagePart.setDataHandler(new DataHandler(fds));
                  imagePart.setHeader("Content-ID","<image>");
                  imagePart.setDisposition(MimeBodyPart.INLINE);
                  multipart.addBodyPart(imagePart);
                  message.setContent(multipart);
                  message.setSubject("Reset Password");
                  message.setRecipients(Message.RecipientType.TO,
                           InternetAddress.parse(to));
                  Transport.send(message);
              } catch (MessagingException e) {
                 throw new RuntimeException(e);
              }
           }

电子邮件快照

4

1 回答 1

5

问题是文件的名称和扩展名未设置,这就是为什么邮件阅读器在添加这一行后将其设置为“noname”,我的问题解决了:)

messageBodyPart.setFileName("logo.png"); 
于 2018-05-24T05:17:44.057 回答