gpt4 book ai didi

java - 从java中的类路径加载图像时出错

转载 作者:行者123 更新时间:2023-12-02 05:35:26 24 4
gpt4 key购买 nike

我正在尝试使用以下代码通过 java mail api 发送图像;

            MimeMessage message = new MimeMessage(mailSession);
message.setSubject(username +"'s Second Story Forgotten Password");
message.setFrom(new InternetAddress(EmailAddress.ADMIN.getValue()));
message.setContent(msg, "text/html");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));


Multipart multipart = new MimeMultipart();


BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(msg, "text/html");
multipart.addBodyPart(htmlPart);

BodyPart imgPart = new MimeBodyPart();
DataSource ds = getImage();
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "the-img-1");
multipart.addBodyPart(imgPart);
message.setContent(multipart);
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));

ds = getImage 如下所示

private static DataSource getImage(){
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = MailSender.class.getClassLoader();
}
DataSource ds = new FileDataSource("/VimbaEmailLogo.png");
return ds;
}

我有一个资源文件夹,其中包含我的图像,该文件夹已添加到类路径中。我知道这是正确完成的,因为我可以从这里加载其他文件。

每次我尝试加载 png 并发送电子邮件时,都会收到以下错误

DEBUG SMTP: IOException while sending, closing, THROW: 
java.io.FileNotFoundException: /VimbaEmailLogo.png (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at javax.activation.FileDataSource.getInputStream(FileDataSource.java:97)
at javax.activation.DataHandler.writeTo(DataHandler.java:305)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1608)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:961)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:553)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:103)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:889)
at javax.activation.DataHandler.writeTo(DataHandler.java:317)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1608)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1849)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1196)
at com.secondstory.mailsender.MailSender.sendSimpleMessage(MailSender.java:75)
at com.secondstory.mailsender.MailSender.generateLostPasswordEmail(MailSender.java:124)
at com.secondstory.mailsender.MailSender.main(MailSender.java:149)

我是否正确加载图像以发送电子邮件 - 如果没有,我需要如何更改它?谢谢

最佳答案

您获得了一个类加载器,但随后尝试使用FileDataSource从文件系统加载图像。目的。将其更改为 URLDataSource并从类加载器获取的 URL 加载图像。

private static DataSource getImage() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = MailSender.class.getClassLoader();
}
DataSource ds = new URLDataSource(classLoader.getResource("VimbaEmailLogo.png"));
return ds;
}

关于java - 从java中的类路径加载图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25013363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com