Embedding inline image in email and referring it thru cid?

人盡茶涼 提交于 2019-12-25 05:23:16

问题


i am sending the inline image with email with mime message. Here is the brief code for the same. This is working fine. My question is i am not setting the MimeMessage content-type as multipart/related (Also not setting multipart subtype as related)still my code is working fine and iam able to get the inline image at expected postion. Should i really care about setting the Content-Type as multipart/related when i am referring the image part with cid or server takes care of that?

  MimeMessage   msg = new MimeMessage(mailSession);
  MimeMultipart mpart = new MimeMultipart();
  MimeBodyPart bp = new MimeBodyPart();
  bp.setText("plain text and here is html image refering image part <img src="cid:Unique-ContentId" />", CHARSET_UTF_8, MESSAGE_HTML_CONTENT_TYPE);
  // add message body
  mpart.addBodyPart(bp);

 // adding inline image  part
  MimeBodyPart bodyPart1 = new MimeBodyPart();
  bodyPart1.setFileName("inline image");
  file1 = new File("image1");
  DataSource source1 = new FileDataSource(file);
  bodyPart1.setDataHandler(new DataHandler(source));
  bodyPart1.setDisposition(MimeBodyPart.INLINE);
  bodyPart1.setHeader("Content-ID", "Unique-ContentId");
  bodyPart1.setHeader("Content-Type", "image/jpeg");
  mpart.addBodyPart(bodyPart1);

  // At last setting multipart In MimeMessage
  msg.setContent(mpart);

Just for information my email client can be outlook,lotusnotes,yahoo,gmail,thunderbird


回答1:


That's what we call "luck". :-)

Apparently the email clients you're using are being very generous in the way they interpret the message you're sending. There's nothing in the email specs that suggest they should interpret such messages in this way.



来源:https://stackoverflow.com/questions/13157171/embedding-inline-image-in-email-and-referring-it-thru-cid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!