how to embed multiple images while sending html email using Java mail
-
I'm trying to send email from javamail. I'm embeding the images and using CID. But the problem is how do I embed multiple images in a single message. if I try to add in header.. it is just taking the last set header. how do I add multiple images and reference using CID. MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); // BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><br/> <p align=center><img src=\"cid:senny\"> </p>"; htmlText+="<p align=center><img src=\"cid:senny\"> </p>"; htmlText+="<p align=center><img src=\"cid:image\"> </p>"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\images\\cec_header_457.png"); DataSource fds1 = new FileDataSource ("C:\\images\\cec_header_420.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setDataHandler(new DataHandler(fds1)); messageBodyPart.addHeader("Content-ID","<image>"); messageBodyPart.addHeader("Content-ID","<senny>"); // add it multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart);
-
Answer:
Each image needs to be its own MimeBodyPart, break up this code, // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\images\\cec_header_457.png"); DataSource fds1 = new FileDataSource ("C:\\images\\cec_header_420.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setDataHandler(new DataHandler(fds1)); messageBodyPart.addHeader("Content-ID","<image>"); messageBodyPart.addHeader("Content-ID","<senny>"); // add it multipart.addBodyPart(messageBodyPart); Into two multi parts, something like // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds1 = new FileDataSource ("C:\\images\\cec_header_420.png"); messageBodyPart.setDataHandler(new DataHandler(fds1)); messageBodyPart.addHeader("Content-ID","<senny>"); // add it multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\images\\cec_header_457.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.addHeader("Content-ID","<image>"); // add it multipart.addBodyPart(messageBodyPart);
user57421 at Stack Overflow Visit the source
Related Q & A:
- How to drag multiple images in Android?Best solution by Stack Overflow
- How to store multiple Images in database?Best solution by Stack Overflow
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- How to embed (almost) any document to HTML?Best solution by Stack Overflow
- How to Embed powerpoint in html?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.