SFAH
SFAH

Reputation: 534

JavaMail: inline image in email

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("[email protected]"));
                  message.addRecipient(Message.RecipientType.TO,
                       new InternetAddress("[email protected]"));

                  // 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

Upvotes: 4

Views: 7440

Answers (1)

SFAH
SFAH

Reputation: 534

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"); 

Upvotes: 6

Related Questions