corrected UTF-8 handling
added multipart/alternative text / html mail support
This commit is contained in:
@ -10,7 +10,6 @@ import javax.ejb.EJB;
|
|||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.faces.context.ExternalContext;
|
import javax.faces.context.ExternalContext;
|
||||||
import javax.faces.context.FacesContext;
|
import javax.faces.context.FacesContext;
|
||||||
import javax.mail.BodyPart;
|
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Multipart;
|
import javax.mail.Multipart;
|
||||||
@ -29,7 +28,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
@Stateless
|
@Stateless
|
||||||
public class MailService {
|
public class MailService {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);
|
||||||
|
|
||||||
@EJB
|
@EJB
|
||||||
@ -43,8 +42,8 @@ public class MailService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void sendTestHtmlEmail(String recipient) throws MailException {
|
public void 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");
|
||||||
@ -54,9 +53,10 @@ public class MailService {
|
|||||||
public void sendMail(String recipient, String subject, String body) throws MailException {
|
public void sendMail(String recipient, String subject, String body) throws MailException {
|
||||||
try {
|
try {
|
||||||
MimeMessage message = new MimeMessage(mailSession);
|
MimeMessage message = new MimeMessage(mailSession);
|
||||||
message.setSubject(subject);
|
message.setSubject(subject, "UTF-8");
|
||||||
|
message.setFrom();
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
||||||
message.setText(body);
|
message.setText(body, "UTF-8");
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
} catch (MessagingException ex) {
|
} catch (MessagingException ex) {
|
||||||
throw new MailException("Error while sending email.", ex);
|
throw new MailException("Error while sending email.", ex);
|
||||||
@ -64,26 +64,35 @@ public class MailService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
|
public void sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String templateName) throws MailException {
|
||||||
|
sendHTMLMail(recipient, subject, dataModel, templateName, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void 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.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
|
||||||
message.setSubject(subject);
|
message.setSubject(subject);
|
||||||
|
|
||||||
String body = mailTemplateService.getStringFromTemplate(templateName, dataModel);
|
String htmlBody = mailTemplateService.getStringFromTemplate(htmlTemplateName, dataModel);
|
||||||
|
MimeBodyPart htmlBodyPart = new MimeBodyPart();
|
||||||
|
htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8");
|
||||||
|
|
||||||
BodyPart bodyPart = new MimeBodyPart();
|
Multipart multipart = new MimeMultipart("alternative");
|
||||||
bodyPart.setContent(body, "text/html");
|
if (plainTemplateName != null) {
|
||||||
|
String plainBody = mailTemplateService.getStringFromTemplate(plainTemplateName, dataModel);
|
||||||
Multipart multipart = new MimeMultipart();
|
MimeBodyPart plainBodyPart = new MimeBodyPart();
|
||||||
multipart.addBodyPart(bodyPart);
|
plainBodyPart.setText(plainBody, "UTF-8");
|
||||||
message.setContent(multipart, "text/html; charset=UTF-8");
|
multipart.addBodyPart(plainBodyPart);
|
||||||
|
}
|
||||||
|
multipart.addBodyPart(htmlBodyPart);
|
||||||
|
message.setContent(multipart);
|
||||||
|
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
LOGGER.info ("Mail sent to "+recipient);
|
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 void sendPasswortResetStartEmail(AccountEntity account, String token) throws MailException {
|
||||||
|
|||||||
@ -4,9 +4,7 @@ import de.muehlencord.shared.account.entity.MailTemplateEntity;
|
|||||||
import freemarker.cache.StringTemplateLoader;
|
import freemarker.cache.StringTemplateLoader;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
|
||||||
import freemarker.template.TemplateExceptionHandler;
|
import freemarker.template.TemplateExceptionHandler;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
|
|||||||
Reference in New Issue
Block a user