Java不能发送包含图像的HTML电子邮件 [英] Java cannot send email with html with images
问题描述
我试图用包含两个图像的html发送电子邮件。这两个图像是从AngularJS客户端以base64字符串形式发送的,如下所示:
data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAA + gAAALuCAYAAAA9jTxNAAAgAElEQ
请注意,我截断了base64字符串太长。
String temp = baseString.split(,)[1];
byte [] tile = DatatypeConverter.parseBase64Binary(temp);
BodyPart messageBodyPart = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(tile);
DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream));
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setHeader(Content-ID,< image>);
multipart.addBodyPart(messageBodyPart);
InputStreamDataSource:
public class InputStreamDataSource implements DataSource {
private InputStream inputStream;
public InputStreamDataSource(InputStream inputStream){
this.inputStream = inputStream;
}
public InputStream getInputStream(){
return inputStream;
}
$ b $ public OutputStream getOutputStream()throws IOException {
throw new UnsupportedOperationException(Not implemented);
}
public String getContentType(){
return* / *;
}
public String getName(){
returnInputStreamDataSource;
邮件中没有显示图片。
但是,当我使用 FileDataSource
而不是base64字符串时它工作得很好:
DataSource fds = new FileDataSource(D:\\Projects\\Extras\\sofa1.png);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader(Content-ID,< image>);
multipart.addBodyPart(messageBodyPart);
这可以正常工作并显示图像。
<有人请帮我解决这个问题。 使用 /util/ByteArrayDataSource.htmlrel =nofollow noreferrer> ByteArrayDataSource 而不是您自己的InputStreamDataSource。
您的代码只显示一个图像;希望你为每个图像使用不同的内容ID。
你的代码也不应该如何HTML内容引用图像;希望它使用正确的cid:URL。
JavaMail FAQ有更多关于如何发送包含图片的邮件。
I am trying to send email with html which has two images. The two images are sent from the AngularJS client side as base64 strings and looks like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAALuCAYAAAA9jTxNAAAgAElEQ
Note that I have truncated the base64 string as its too long.
String temp = baseString.split(",")[1];
byte[] tile = DatatypeConverter.parseBase64Binary(temp);
BodyPart messageBodyPart = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(tile);
DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream));
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);
The InputStreamDataSource:
public class InputStreamDataSource implements DataSource {
private InputStream inputStream;
public InputStreamDataSource(InputStream inputStream) {
this.inputStream = inputStream;
}
public InputStream getInputStream() {
return inputStream;
}
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
public String getContentType() {
return "*/*";
}
public String getName() {
return "InputStreamDataSource";
}
}
The image does not show up in the mail.
But it works perfectly fine when I use a FileDataSource
instead of base64 string:
DataSource fds = new FileDataSource("D:\\Projects\\Extras\\sofa1.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);
This works fine and shows the images.
Someone please help me fix this.
Use ByteArrayDataSource instead of your own InputStreamDataSource.
Your code only shows one image; hopefully you're using different Content-IDs for each image.
Your code also doesn't should how the html content is referencing the images; hopefully it's using the correct "cid:" URL.
The JavaMail FAQ has more information on how to send messages that include images.
这篇关于Java不能发送包含图像的HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!