首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正在发送带有图像的HTML邮件,添加附件"noname“

正在发送带有图像的HTML邮件,添加附件"noname“
EN

Stack Overflow用户
提问于 2018-07-15 12:08:11
回答 1查看 937关注 0票数 0

我正在尝试发送一封电子邮件,使用javax.mail。

问题是,我在html代码中有2个图像,这些图像在邮件正文中显示正确。

但我也有2个文件(“无名”)与这封电子邮件附件。

代码如下:

代码语言:javascript
复制
   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);

谁能给出答案?

非常感谢你的回复。

EN

回答 1

Stack Overflow用户

发布于 2018-07-17 03:53:08

multipart/related应该嵌套在multipart/mixed中,其中还包含附件:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51345165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档