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