0

I've been stuck with a strange problem for a while now and I really don't see anything wrong in my code. I'm trying to send an email with java where I attach an image to the message. First I have the message with plain text, and then the image by attaching it and using html to display it.

This is what I think it should look like:

+-----------------------------------------------+ 
| multipart/related                             | 
| +---------------------------+  +------------+ | 
| |multipart/alternative      |  | image/gif  | | 
| | +-----------+ +---------+ |  |            | | 
| | |text/plain | |text/html| |  |            | | 
| | +-----------+ +---------+ |  |            | | 
| +---------------------------+  +------------+ | 
+-----------------------------------------------+

Here's my code:

public void sendEmail(Email email) throws MessagingException {
    String[] recipients = email.getRecipients();
    // Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    //props.put("mail.smtps.auth", "true");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    MimeMessage msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(email.getFrom());
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(email.getSubject());

    BodyPart mbp1 = new MimeBodyPart();
    mbp1.setContent(email.getMessage(), "text/plain");

    BodyPart mbp2 = new MimeBodyPart();
    mbp2.setContent("<html><body><br/><br/><br/><img src=\"cid:image"><br/>"
            + "Support<br/>"
            + "Carl Olofsson<br/>"
            + "Management Unit<br/>"
            + "----<br/>"
            + "----<br/>"
            + "Sweden<br/>"
            + "www.-----.com<br/></body></html>", "text/html");

    Multipart contentMultipart = new MimeMultipart("alternative");
    contentMultipart.addBodyPart(mbp1);
    contentMultipart.addBodyPart(mbp2);

    BodyPart contentBodyPart = new MimeBodyPart();
    contentBodyPart.setContent(contentMultipart);

    BodyPart mbp3 = new MimeBodyPart();
    DataSource ds = new FileDataSource("C:/Users/000/Desktop/image.jpg");
    mbp3.setDataHandler(new DataHandler(ds));
    mbp3.setHeader("Content-ID","image");

    Multipart entireMultipart = new MimeMultipart("related");
    entireMultipart.addBodyPart(contentBodyPart);
    entireMultipart.addBodyPart(mbp3);

    // attaching the multi-part to the message
    msg.setContent(entireMultipart);

    // set the message content here
    Transport t = session.getTransport("smtp");
    try {
        t.connect(host, username, password);
        t.sendMessage(msg, msg.getAllRecipients());
    } catch(Throwable throwable){
        LOG.error(throwable.getMessage());
    }finally {
        t.close();
    }
}

}

The message is sent but the only thing that it shows is the "footer" with the image, and not the message!

Can you see something wrong in the code?

Thanks in advance!

user2099024
  • 1,452
  • 4
  • 16
  • 26

1 Answers1

1

The image isn't really related to the text part. Try reversing the multiparts:

multipart/alternative
  text/plain
  multipart/related
    text/html
    image/gif
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40