@Named
@Stateless
public class MailerBean {
@Resource(name="mail/myExampleSession")
private Session session;
private static final Logger logger =
Logger.getLogger(MailerBean.class.getName());
@Asynchronous
public Future<String> sendMessage(String email) {
String status;
try {
Properties properties = new Properties(); properties.put("mail.smtp.port", "3025"); session = Session.getInstance(properties); Message message = new MimeMessage(session);
message.setFrom();
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email, false));
message.setSubject("Test message from async example");
message.setHeader("X-Mailer", "JavaMail");
DateFormat dateFormatter = DateFormat
.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
Date timeStamp = new Date();
String messageBody = "This is a test message from the async "
+ "example of the Java EE Tutorial. It was sent on "
+ dateFormatter.format(timeStamp)
+ ".";
message.setText(messageBody);
message.setSentDate(timeStamp);
Transport.send(message);
status = "Sent";
logger.log(Level.INFO, "Mail sent to {0}", email);
} catch (MessagingException ex) {
logger.severe("Error in sending message.");
status = "Encountered an error: " + ex.getMessage();
logger.severe(ex.getMessage());
}
return new AsyncResult<>(status);
}
}