我使用下面的代码向用户发送电子邮件,它按预期工作。图像显示在电子邮件中的html中。但最近注意到,在点击电子邮件之前,还有一个附件图标。这是一个小问题,但有点烦人。难以理解为什么会发生这种情况以及如何阻止它?有任何想法吗?谢谢。
public static void send(String useremail, String htmlBody,Map<String, String> mapInlineImages, String subject, String internetAddress, String websiteFrom) throws MalformedURLException{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String msgBody = "...";
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(internetAddress, websiteFrom));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(useremail, "Admin"));
msg.setSubject(subject);
msg.setText(msgBody);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(htmlBody, "text/html");
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
if (mapInlineImages != null && mapInlineImages.size() > 0) {
Set<String> setImageID = mapInlineImages.keySet();
for (String contentId : setImageID) {
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<" + contentId + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
String imageFilePath = mapInlineImages.get(contentId);
try {imagePart.attachFile(imageFilePath);
} catch (IOException ex) {ex.printStackTrace();
}multipart.addBodyPart(imagePart);}}
msg.setContent(multipart);
Transport.send(msg);
} catch (AddressException e) {}
catch (MessagingException e) {}
catch (UnsupportedEncodingException e) {}
}
答案 0 :(得分:1)
添加imagePart.addHeader("Content-Type", "image/jpeg");