By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Java mail API problem

P: 1
Hi List,

I am using java mail API. I have to embed an image with my html content.

Here is the sample code that I am using for this.

MimeMultipart multipart = new MimeMultipart("related");
String imagefile = "E:/data//image002.gif";

try {

// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();

String htmlText = "Hello";
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(htmlText,"text/html")));
multipart.addBodyPart(messageBodyPart);

// Create part for the image
messageBodyPart = new MimeBodyPart();

// Fetch the image and associate to part
DataSource fds = new FileDataSource(imagefile);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "test_001");
messageBodyPart.setDisposition("inline");
message.setContent(multipart);


I am able to get an email as required with embedded image in OUTLOOK.
But It is not displaying images inline in gmail and yahoo.


I am unable to get where I am wrong??

Thanks in advance
vikas
Jan 23 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 168
Your Content-ID should be in angle brackets, and you might as well
use the method designed for this purpose:

MimeBodyPart messageBodyPart = new MimeBodyPart();
...
messageBodyPart.setContentID("<test_001>");


That might help.
Jan 30 '07 #2

100+
P: 168
The below code can help you

messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource
("Images/currency.jpg");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");

// add it
multipart.addBodyPart(messageBodyPart);

Source: http://forum.java.sun.com/thread.jspa?threadID=5130082&messageID=9466166#946 6166
Jan 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.