In the afternoon, I tried to make my application sent as html + images via javamail, I only managed to send html, but I had problems with the image. I decided to create a multi-page message and everything went fine, but then I use the class loader to extract the .png file from WEB-INF / resources / images, I get NullPointerExcetion, I donβt know why this is?
This is what my EJB (3.0) looks like. I really appreciate the hand on this, I donβt have much experience with the ClassLoader class (I donβt know much about it).
@Stateless(name = "ejbs/EmailServiceEJB")
public class EmailServiceEJB implements IEmailServiceEJB {
@Resource(name = "mail/myMailSession")
private Session mailSession;
public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name) {
String to = destinationEmail;
String from = "dontreply2thismessage@gmail.com";
try {
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Uspijesna registracija");
message.setContent(generateActivationLinkTemplate(), "text/html");
Date timeStamp = new Date();
message.setSentDate(timeStamp);
Multipart multipart = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
multipart.addBodyPart(htmlPart);
BodyPart imgPart = new MimeBodyPart();
String fileName = "/WEB-INF/resources/images/logoemailtemplate.png";
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
if (classLoader == null) {
System.out.println("IT IS NULL AGAIN!!!!");
}
}
DataSource ds = new URLDataSource(classLoader.getResource(fileName));
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "the-img-1");
multipart.addBodyPart(imgPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
I would like to mention that I use JEE6 with glassfishV3, I do not know if my approach is compatible with this application server.
String fileName = "logoemailtemplate.png";
, .
.:) ?