嗨,我有以下代码:
MimeMessage msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("krao346789@gmail.com");
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[sendTo.length];
for (int i = 0; i < sendTo.length; i++) {
addressTo[i] = new InternetAddress(sendTo[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(emailSubjectTxt);
/*Image part*/
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
String contextPath=request.getContextPath();
System.out.println("contextpath"+contextPath);
File contextDir = new File(contextPath);
System.out.println("contextDir"+contextDir);
File emailImage = new File(contextDir, "/images/sample.jpeg");
System.out.println("emailImage"+emailImage);
DataSource fds = new FileDataSource(emailImage);
//System.out.println("fds"+fds.getName());
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
// add it
multipart.addBodyPart(messageBodyPart);
// put everything together
msg.setContent(multipart);
Transport.send(msg);
}
如果我使用它工作正常
DataSource fds = new FileDataSource("C:\\images\\sample.jpeg");
代替
DataSource fds = new FileDataSource(emailImage);
但我想从WebContent->images
. 当我运行这个时,
我得到了。java.lang.NullPointerException