如何在邮件正文中发送图像而不像Java中的附件

编程入门 行业动态 更新时间:2024-10-09 23:16:08
本文介绍了如何在邮件正文中发送图像而不像Java中的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在做Java Web应用程序,我正在该邮件正文中发送邮件,我需要显示图像,但是当我发送邮件时,它需要图像附件,我不想附件,我需要在正文内容中显示图像,任何人都可以吗?请告诉我该怎么做

I am doing Java Web application, I am sending mails in that mail body i need to display image but when i send mail it takes image attachment i don't want to attachment i need to show the image in body content can anyone please tell me how to do this

推荐答案

如@ scary-wombat所述,您没有添加第一个参数.我想你打算这样做:

As @scary-wombat mentioned, you didn't add the first par. I suppose you meant to do:

... // add it multipart.addBodyPart(messageBodyPart); // second part (the image) ...

您还可以将Content-Disposition标头添加到图像部分:

You can also add a Content-Disposition header to the image part:

messageBodyPart.setDisposition(MimeBodyPart.INLINE);

更新:

对不起,您还必须向上移动多部分的创建:

Sorry, you must also move up the creation of multipart:

... // add it MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(messageBodyPart); // second part (the image) ...

更新2:

尝试一下:

BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); java.io.InputStream inputStream = this.getClass().getResourceAsStream("/HappyBirthday.JPG"); ByteArrayDataSource ds = new ByteArrayDataSource(inputStream, "image/jpg"); System.out.println(inputStream); messageBodyPart.setDataHandler(new DataHandler(ds)); messageBodyPart.setHeader("Content-ID", "<image>"); messageBodyPart.setDisposition(MimeBodyPart.INLINE); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); // Send message Transport.send(message);

更多推荐

如何在邮件正文中发送图像而不像Java中的附件

本文发布于:2023-10-12 04:19:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文中   附件   图像   邮件   而不像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!