0

Here is my code:

MimeMessage mail = new MimeMessage(session);
mail.setFrom(from);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyText, "text/html");

multipart.addBodyPart(htmlPart);

MimeBodyPart imgPart=new MimeBodyPart();
String path = "/ivr/imagelogos/accenture.jpg";
DataSource ds=new FileDataSource(path);
imgPart.setDataHandler(new DataHandler(ds));    
imgPart.setHeader("Content-ID","the-img-1");
multipart.addBodyPart(imgPart);

mail.setContent(multipart);

mail.setSentDate(new Date());
mail.setHeader("X-Mailer", "ALS Notifier Build 1.0.0.10");

// send the message
Transport.send(mail);

The code is being ran on a unix box - image path is based on unix file paths.

After running the code I receive this error:

IOException while sending message
javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        java.io.FileNotFoundException: /ivr/imagelogos/accenture.jpg (No such file or directory)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
Reimius
  • 5,694
  • 5
  • 24
  • 42
  • Can you clarify whether you want to embed the image in the email, or (more likely) create an HTML email that has image references in it? If it's the 2nd one, you don't need to actually put the image in the email. You just need the public URL of the image on a server somewhere, and in your HTML make an img element with that src. – jfrank Apr 12 '13 at 21:06

2 Answers2

2

Sounds like the /ivr/imagelogos/accenture.jpg file doesn't exist. Are you sure that's the right path? Maybe it's supposed to be relative to some other path? If it does exist, does the user running the Java app have read permissions on it?

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
1

Img src= is the most efficient way to insert just a few images, otherwise you may find it useful/helpful to define an array for multiple images.

laaposto
  • 11,835
  • 15
  • 54
  • 71
Ms. Art
  • 11
  • 1