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

时间:2014-07-29 10:40:10

标签: java image email classpath javax.mail

我正在尝试使用以下代码通过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)

我是否正确加载图片以发送电子邮件 - 如果不是,我该如何更改?感谢

1 个答案:

答案 0 :(得分:2)

您正在获取类加载器,但随后尝试使用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;
}