added message id support

This commit is contained in:
jomu
2017-01-21 22:40:18 +00:00
parent dc91c32499
commit 5b35c6bc01

View File

@ -37,37 +37,37 @@ public class MailService {
@Resource(lookup = "java:jboss/mail/ssgMail") @Resource(lookup = "java:jboss/mail/ssgMail")
private Session mailSession; private Session mailSession;
public void sendTestEmail(String recipient) throws MailException { public String sendTestEmail(String recipient) throws MailException {
sendMail(recipient, "Test email", "This is a test email"); return sendMail(recipient, "Test email", "This is a test email");
} }
public void sendTestHtmlEmail(String recipient) throws MailException { public String sendTestHtmlEmail(String recipient) throws MailException {
Date now = new Date(); Date now = new Date();
AccountEntity account = new AccountEntity(UUID.randomUUID(), "joern.muehlencord", "joern@muehlencord.de", "Jörn", "Mühlencord", "secret", 0, "NEW", false, now, "admin", now, "admin"); AccountEntity account = new AccountEntity(UUID.randomUUID(), "joern.muehlencord", "joern@muehlencord.de", "Jörn", "Mühlencord", "secret", 0, "NEW", false, now, "admin", now, "admin");
MailDatamodel dataModel = new MailDatamodel(account); MailDatamodel dataModel = new MailDatamodel(account);
dataModel.addParameter("url", "http://url.de"); dataModel.addParameter("url", "http://url.de");
dataModel.addParameter("resetUrl", "http://reseturl.de"); dataModel.addParameter("resetUrl", "http://reseturl.de");
sendHTMLMail(recipient, "Test HTML Email", dataModel, "password_reset_html"); return sendHTMLMail(recipient, "Test HTML Email", dataModel, "password_reset_html");
} }
public void sendMail(String recipient, String subject, String body) throws MailException { public String sendMail(String recipient, String subject, String body) throws MailException {
try { try {
MimeMessage message = new MimeMessage(mailSession); MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject, "UTF-8"); message.setSubject(subject, "UTF-8");
message.setFrom(); message.setFrom();
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
message.setText(body, "UTF-8"); message.setText(body, "UTF-8");
Transport.send(message); return transportMail(message);
} catch (MessagingException ex) { } catch (MessagingException ex) {
throw new MailException("Error while sending email.", ex); throw new MailException("Error while sending email.", ex);
} }
} }
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException { public String sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
sendHTMLMail(recipient, subject, dataModel, templateName, null); return sendHTMLMail(recipient, subject, dataModel, templateName, null);
} }
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName) throws MailException { public String sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName) throws MailException {
try { try {
Message message = new MimeMessage(mailSession); Message message = new MimeMessage(mailSession);
message.setFrom(); // use default from message.setFrom(); // use default from
@ -88,14 +88,13 @@ public class MailService {
multipart.addBodyPart(htmlBodyPart); multipart.addBodyPart(htmlBodyPart);
message.setContent(multipart); message.setContent(multipart);
Transport.send(message); return transportMail(message);
LOGGER.info("Mail sent to " + recipient);
} catch (MessagingException | MailTemplateException ex) { } catch (MessagingException | MailTemplateException ex) {
throw new MailException("Error while sending email.", ex); throw new MailException("Error while sending email.", ex);
} }
} }
public void sendPasswortResetStartEmail(AccountEntity account, String token) throws MailException { public String sendPasswortResetStartEmail(AccountEntity account, String token) throws MailException {
MailDatamodel model = new MailDatamodel(account); MailDatamodel model = new MailDatamodel(account);
try { try {
@ -115,6 +114,14 @@ public class MailService {
throw new MailException("Error while sending email.", ex); throw new MailException("Error while sending email.", ex);
} }
sendHTMLMail(account.getEmailaddress(), "Reset your password", model, "password_reset_html"); return sendHTMLMail(account.getEmailaddress(), "Reset your password", model, "password_reset_html");
}
private String transportMail(Message message) throws MessagingException {
Transport.send(message);
String messageId = message.getHeader("Message-ID")[0];
LOGGER.info("Mail sent to {}, messageid = {}", message.getAllRecipients()[0].toString(), messageId);
return messageId;
} }
} }