added message id support
This commit is contained in:
@ -37,37 +37,37 @@ public class MailService {
|
||||
@Resource(lookup = "java:jboss/mail/ssgMail")
|
||||
private Session mailSession;
|
||||
|
||||
public void sendTestEmail(String recipient) throws MailException {
|
||||
sendMail(recipient, "Test email", "This is a test email");
|
||||
public String sendTestEmail(String recipient) throws MailException {
|
||||
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();
|
||||
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);
|
||||
dataModel.addParameter("url", "http://url.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 {
|
||||
MimeMessage message = new MimeMessage(mailSession);
|
||||
message.setSubject(subject, "UTF-8");
|
||||
message.setFrom();
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
||||
message.setText(body, "UTF-8");
|
||||
Transport.send(message);
|
||||
return transportMail(message);
|
||||
} catch (MessagingException ex) {
|
||||
throw new MailException("Error while sending email.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
|
||||
sendHTMLMail(recipient, subject, dataModel, templateName, null);
|
||||
public String sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
|
||||
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 {
|
||||
Message message = new MimeMessage(mailSession);
|
||||
message.setFrom(); // use default from
|
||||
@ -87,15 +87,14 @@ public class MailService {
|
||||
}
|
||||
multipart.addBodyPart(htmlBodyPart);
|
||||
message.setContent(multipart);
|
||||
|
||||
Transport.send(message);
|
||||
LOGGER.info("Mail sent to " + recipient);
|
||||
|
||||
return transportMail(message);
|
||||
} catch (MessagingException | MailTemplateException 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);
|
||||
|
||||
try {
|
||||
@ -115,6 +114,14 @@ public class MailService {
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user