本文介绍了如何在邮件正文中发送图像而不像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中的附件
发布评论