更新时间:2022-11-07 21:15:56
如@ 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);