我正在尝试发送一封电子邮件,使用javax.mail。
问题是,我在html代码中有2个图像,这些图像在邮件正文中显示正确。
但我也有2个文件(“无名”)与这封电子邮件附件。
代码如下:
BodyPart messageBodyPart ;
// Add HTML + image
// first part (the html)
messageBodyPart = new MimeBodyPart();
//multipart = new MimeMultipart("related");
messageBodyPart.setContent(Constantes.html + msg +"</h4>", "text/html");
// add it
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
//multipart.addBodyPart(messageBodyPart);
// second part ( image 1)
BodyPart messageBodyPartSMSC = new MimeBodyPart();
DataSource fds = new FileDataSource(Constantes.imagePath + "logo1.png");
messageBodyPartSMSC.setDataHandler(new DataHandler(fds));
messageBodyPartSMSC.setHeader("Content-ID", "<logo1>");
// add image to the multipart
messageBodyPartSMSC.setDisposition(MimeBodyPart.INLINE);
// multipart.addBodyPart(messageBodyPart);
// second part ( image 2)
BodyPart messageBodyPartDEVB = new MimeBodyPart();
fds = new FileDataSource(Constantes.imagePath + "logo2.png");
messageBodyPartDEVB.setDataHandler(new DataHandler(fds));
messageBodyPartDEVB.setHeader("Content-ID", "<logo2>");
messageBodyPartDEVB.setDisposition(MimeBodyPart.INLINE);
// add image to the multipart
//multipart.addBodyPart(messageBodyPart);
// add attachment (zip file) ------------------------------
DataSource source = new FileDataSource(fileName);
BodyPart messageBodyPartZip = new MimeBodyPart();
messageBodyPartZip.setDataHandler(new DataHandler(source));
messageBodyPartZip.setFileName(onlyFileName);
messageBodyPartZip.setDisposition(MimeBodyPart.ATTACHMENT);
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPartSMSC);
multipart.addBodyPart(messageBodyPartDEVB);
multipart.addBodyPart(messageBodyPartZip);
// Send the complete message parts
message.setContent(multipart);
Transport.send(message);
谁能给出答案?
非常感谢你的回复。
发布于 2018-07-17 03:53:08
multipart/related应该嵌套在multipart/mixed中,其中还包含附件:
BodyPart messageBodyPart ;
// Add HTML + image
// first part (the html)
messageBodyPart = new MimeBodyPart();
MimeMultipart related = new MimeMultipart("related");
messageBodyPart.setContent(Constantes.html + msg +"</h4>", "text/html");
// add it
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
related.addBodyPart(messageBodyPart);
// second part ( image 1)
BodyPart messageBodyPartSMSC = new MimeBodyPart();
DataSource fds = new FileDataSource(Constantes.imagePath + "logo1.png");
messageBodyPartSMSC.setDataHandler(new DataHandler(fds));
messageBodyPartSMSC.setHeader("Content-ID", "<logo1>");
// add image to the multipart
messageBodyPartSMSC.setDisposition(MimeBodyPart.INLINE);
related.addBodyPart(messageBodyPart);
// second part ( image 2)
BodyPart messageBodyPartDEVB = new MimeBodyPart();
fds = new FileDataSource(Constantes.imagePath + "logo2.png");
messageBodyPartDEVB.setDataHandler(new DataHandler(fds));
messageBodyPartDEVB.setHeader("Content-ID", "<logo2>");
messageBodyPartDEVB.setDisposition(MimeBodyPart.INLINE);
// add image to the multipart
related.addBodyPart(messageBodyPart);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart rbp = new MimeBodyPart();
rbp.setContent(related);
multipart.addBodyPart(rbp);
// add attachment (zip file) ------------------------
BodyPart messageBodyPartZip = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPartZip.setDataHandler(new DataHandler(source));
messageBodyPartZip.setFileName(onlyFileName);
messageBodyPartZip.setDisposition(MimeBodyPart.ATTACHMENT);
// or replace the above 4 lines with:
// messageBodyPartZIP.attachFile(fileName);
multipart.addBodyPart(messageBodyPartZip);
// Send the complete message parts
message.setContent(multipart);
Transport.send(message);
您还可以通过将图像作为内联数据包含在html中来简化结构。在这种情况下,您根本不需要multipart/related,可以将html body部分添加为multipart/mixed中的第一个body部分。有关详细信息,请参阅JavaMail FAQ。
https://stackoverflow.com/questions/51345165
复制相似问题