Sending mail along with embedded image using javam

2020-02-15 02:27发布

问题:

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??

回答1:

I've stumbled on the similar problem. Following post helped me a lot: How to send email with embedded images using Java The most important part of the code is:

String cid = generateCID();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("<html><head>"
+ "<title>This is not usually displayed</title>"
+ "</head>n"
+ "<body><div><strong>Hi there!</strong></div>"
+ "<div>Sending HTML in email is so <em>cool!</em> </div>n"
+ "<div>And here's an image: <img src=\"cid:\"" + cid + " /></div>" 
+ "<div>I hope you like it!</div></body></html>",
"US-ASCII", "html");
content.addBodyPart(textPart);

MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("resources/teapot.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);

Function generateCID() must return unique String. For example:

java.util.UUID.randomUUID()


回答2:

Change the "relative" to "alternative", then you will get the image as an attachment.

    Multipart multipart = new MimeMultipart("alternative");


回答3:

Try getting rid of the following line:

messageBodyPart.setDisposition("inline");


回答4:

Change new MimeMultipart("related"); to new MimeMultipart(); (and optionally msg.setContent(multipart); to msg.setContent(multipart,"multipart/related");) Also make sure you change img src=\"cid:BarcodeImage\" to img src="cid:BarcodeImage". It should work then.