JavaMail:电子邮件中的内嵌图像

时间:2018-05-23 05:36:13

标签: java html javamail email-attachments inline-images

我想在邮件正文中附加一个内嵌图片我使用以下方法一切都很好,但是附有'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);
              }
           }

SnapShot of Email

1 个答案:

答案 0 :(得分:2)

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

18/05/21 22:55:55 INFO mapreduce.ImportJobBase: Transferred 0 bytes in 47.9229 seconds (0 bytes/sec)
18/05/21 22:55:55 INFO mapreduce.ImportJobBase: Retrieved 33372 records.