Text format + attached image files or Html format + displayed images ?
843834Mar 20 2008 — edited Jun 9 2008Hi, I have this kind of probleme and I need some help.
(Code : see below)
I want to send a mail with some attached image files.
For the message part, I create a MimeBodyPart and add two kinds of message (text or html) and this part is multipart/alternative.
For the attachment part, I create another MimeBodyPart and I set the Header like this : xxx.setHeader("Content-ID", "<image>").
When I use outlook (html format) , I receive a mail with the picture displayed at the bottom, it's perfect.
However the problem is for the mailbox which is in text mode, I receive a mail with message + attachment files, but I also have some image frames generated by xxx.setHeader("Content-ID", "<image>") I guess.
Who can give me a hand to implement something that able to make difference according to text format or html format.
I post my code here, thanks a lot :
//set Message
msg.setText(pMsgText, DEFAULT_CHARSET);
// Partie Texte brut
MimeBodyPart textPart = new MimeBodyPart();
String header = "";
textPart.setText(header + pMsgText, DEFAULT_CHARSET);
textPart.setHeader(
"Content-Type",
"text/plain;charset=\"iso-8859-1\"");
textPart.setHeader("Content-Transfert-Encoding", "8bit");
// Partie HTML
MimeBodyPart htmlPart = new MimeBodyPart();
String images = BTSBackUtil.generateImgSrc(pictureContainers.length);
String endPart = "</div>\n</body>\n</html>";
htmlPart.setText(pMsgTextHTML+images+endPart);
htmlPart.setHeader("Content-Type", "text/html");
// create the mail root multipart
// Multipartie
Multipart multipart = new MimeMultipart("mixed");
// create the content miltipart (for text and HTML)
MimeMultipart mpContent = new MimeMultipart("alternative");
// create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
contentPartRoot.setContent(mpContent);
// add the root body part to the root multipart
multipart.addBodyPart(contentPartRoot);
// add text
mpContent.addBodyPart(textPart);
// add html
mpContent.addBodyPart(htmlPart);
// this part treates attachment
if (pictureContainers != null){
PictureContainer pictureContainer = new PictureContainer();
String fileName = "";
for(int i = 0; i < pictureContainers.length; i++) {
pictureContainer = pictureContainers;
byte[] bs = pictureContainer.getImgData();
fileName = "image" + i +".jpg";
DataSource dataSource = new ByteArrayDataSource(fileName, "image/jpeg", bs);
DataHandler dataHandler = new DataHandler(dataSource);
MimeBodyPart mbp = new MimeBodyPart();
mbp.setDataHandler(dataHandler);
mbp.setHeader("Content-ID", "<image"+i+">");
mbp.setFileName(fileName);
multipart.addBodyPart(mbp);
}
}
// end attachment
msg.setContent(multipart);