corrected UTF-8 handling

added multipart/alternative text / html mail support
This commit is contained in:
jomu
2017-01-08 15:59:16 +00:00
parent 48a7cc142c
commit a751e951da
2 changed files with 24 additions and 17 deletions

View File

@ -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;
@ -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 {

View File

@ -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;