4

I want to attached a inline image in mail body for this I am using the following method everything is fine but an attachment with 'noname' attached to email,I don't want any attachment in email I want to only display an image in email body , following is my code and the snapshot of the mail

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

SFAH
  • 534
  • 1
  • 8
  • 24
  • Your code generally looks fine. How the message is displayed is up to the mail reader you're using and different mail readers will handle this differently. You might try using an embedded image as described in the [JavaMail FAQ](https://javaee.github.io/javamail/FAQ#sendmpr). – Bill Shannon May 23 '18 at 15:54
  • @BillShannon My problem solved now Thankyou for your help :) – SFAH May 24 '18 at 05:18

1 Answers1

6

The problem was that the name and extension of file is not set that's why mail reader sets it "noname" after adding this one line my problem solved :)

messageBodyPart.setFileName("logo.png"); 
SFAH
  • 534
  • 1
  • 8
  • 24