我想连同嵌入的图像一起发送邮件。为此,我使用了以下代码。它不是完整的代码。它是代码的一部分
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);
我面临的问题是,我可以收到邮件,但无法看到图像。它没有显示在邮件中。
下面是我的 html 文件的一部分
<img src=\"cid:BarcodeImage\" alt="Barcode" width="166" height="44" align="right" />
请帮助我为什么图像没有显示在邮件中以及为什么它不在附件中?