Sending mail along with embedded image using javamail

Posted by Raja D on Stack Overflow See other posts from Stack Overflow or by Raja D
Published on 2009-06-10T07:47:55Z Indexed on 2010/03/13 14:05 UTC
Read the original article Hit count: 776

Filed under:
|
|

Hi All:
I want to send mail along with embedded image. For that i have used the below code. Its not full code. Its a part of code

        Multipart multipart = new MimeMultipart("related");
		// Create the message part 
		BodyPart messageBodyPart;
		messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(msgBody); // msgbody contains the contents of the html file
		messageBodyPart.setHeader("Content-Type", "text/html");
		multipart.addBodyPart(messageBodyPart);

		//add file attachments
		DataSource source;
		File file = new File("D:/sample.jpeg");
		if(file.exists()){
			// add attachment
			messageBodyPart = new MimeBodyPart();
			source = new FileDataSource(file);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(file.getName());
			messageBodyPart.setHeader("Content-ID", "<BarcodeImage>");
			messageBodyPart.setDisposition("inline");
			multipart.addBodyPart(messageBodyPart);
		}

		// Put parts in message
		msg.setContent(multipart);
		Transport.send(msg);

Problem i am facing is, i can get the mail but cant acle to see the image.. Its not get displaying in the mail.
Below is my part of html file

             <img src=\"cid:BarcodeImage\" alt="Barcode" width="166" height="44" align="right" />

Please help me why the image not getting displayed in the mail and why it is not in the attachment??

© Stack Overflow or respective owner

Related posts about java

Related posts about javamail