使用javamail发送邮件以及嵌入的图像 [英] Sending mail along with embedded image using javamail

查看:120
本文介绍了使用javamail发送邮件以及嵌入的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送邮件和嵌入的图像。因为我已经使用了下面的代码。它不是全码。它是代码的一部分

  Multipart multipart = new MimeMultipart(related); 
//创建消息部分
BodyPart messageBodyPart;
messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(msgBody); // msgbody包含html文件的内容
messageBodyPart.setHeader(Content-Type,text / html);
multipart.addBodyPart(messageBodyPart);

//添加文件附件
DataSource源;
文件文件=新文件(D:/sample.jpeg);
if(file.exists()){
//添加附件
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);
}

//将消息放在消息中
msg.setContent(multipart);
Transport.send(msg);

我正在面对的问题是,我可以收到邮件但不能看到图像..它

以下是我的html文件的一部分

  img src = \cid:BarcodeImage\alt =条形码width =166height =44align =right/> 

请帮助我为什么图像不会显示在邮件中,为什么它不在附件中?

解决方案

更改新的MimeMultipart(related); code> new MimeMultipart(); (和可选 msg.setContent(multipart); msg.setContent (multipart,multipart / related);
还要确保更改 img src = \cid:BarcodeImage\ to img src =cid:BarcodeImage
它应该工作。


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

解决方案

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.

这篇关于使用javamail发送邮件以及嵌入的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆