首页
学习
活动
专区
工具
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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51345165

复制
相关文章

相似问题

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