萨拉姆,我有一个在网格窗格中生成时间制表的应用程序,可以将网格窗格转换为图像以进行打印,但是现在我想通过电子邮件发送该图像,但是我找不到方法我看过类似的主题,但是它们按计算机的路径发送存在于计算机中的图像,但就我而言,我不想保存图像,我只想直接通过Java类Image或ImageView发送它这是我的代码
@FXML
void send_mail(ActionEvent event) throws IOException {
final String username = "******@gmail.com";
final String password = "******";
Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
javax.mail.Session session = javax.mail.Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("*****@yahoo.com"));
message.setSubject("Testing Subject");
message.setText("PFA");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(image, "IMAGEVIEW");
// String file = "../emploi/"+fileName;
messageBodyPart.setText("Emploi de temps");
//DataSource source = new FileDataSource(file);
// messageBodyPart.setDataHandler(new DataHandler((DataSource) file));
// messageBodyPart.attachFile(file);
messageBodyPart.setFileName("Emploi de temps");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart photoBodyPart = new MimeBodyPart();
photoBodyPart = new MimeBodyPart();
photoBodyPart.setContent(image, "IMAGEVIEW");
multipart.addBodyPart(photoBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
但我收到此错误
javax.mail.internet.ParseException:在Content-Type字符串中, 预期的'/',在处为null javax.mail.internet.ContentType。(ContentType.java:104)在 javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1510) 在 javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1172) 在 javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:522) 在 javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1533) 在 javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2271) 在javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2231) 在javax.mail.Transport.send(Transport.java:123)
[请检查下面的代码,有关更多信息,请访问:Inline images in email using JavaMail
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"image.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");