added email attachment support

This commit is contained in:
jomu
2017-09-17 14:56:37 +00:00
parent 1e961727c0
commit a3bee0a7cf

View File

@ -20,7 +20,12 @@ import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.muehlencord.shared.account.configuration.AccountConfigurationValue;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
*
@ -79,14 +84,15 @@ public class MailService implements Serializable {
}
public String sendHTMLMail(String recipient, String subject, MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName) throws MailException {
return sendHTMLMail(recipient, null, null, subject, dataModel, htmlTemplateName, plainTemplateName, "UTF-8");
return sendHTMLMail(recipient, null, null, subject, dataModel, htmlTemplateName, plainTemplateName, "UTF-8", new ArrayList<>());
}
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject, MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName) throws MailException {
return sendHTMLMail(recipient, ccRecipient, bccRecipient, subject, dataModel, htmlTemplateName, plainTemplateName, "UTF-8");
return sendHTMLMail(recipient, ccRecipient, bccRecipient, subject, dataModel, htmlTemplateName, plainTemplateName, "UTF-8", new ArrayList<>());
}
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject, MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName, String encoding) throws MailException {
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject,
MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName, String encoding, List<File> attachments) throws MailException {
try {
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(); // use default from
@ -100,20 +106,45 @@ public class MailService implements Serializable {
message.setSubject(subject, encoding);
Multipart contentMultiPart = new MimeMultipart("alternative");
String htmlBody = mailTemplateService.getStringFromTemplate(htmlTemplateName, dataModel);
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8");
contentMultiPart.addBodyPart(htmlBodyPart);
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);
contentMultiPart.addBodyPart(plainBodyPart);
}
if ((attachments == null) || (attachments.isEmpty())) {
message.setContent(contentMultiPart);
} else {
MimeBodyPart contentBodyPart = new MimeBodyPart();
contentBodyPart.setContent(contentMultiPart);
MimeMultipart messageMultipart = new MimeMultipart("related");
messageMultipart.addBodyPart(contentBodyPart);
for (File attachment : attachments) {
try {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.attachFile(attachment);
String contentType = Files.probeContentType(attachment.toPath());
if (contentType != null) {
attachmentBodyPart.setHeader("Content-Type", contentType);
}
messageMultipart.addBodyPart(attachmentBodyPart);
} catch (IOException ex) {
throw new MailException("Cannot attach " + attachment.toString() + " to email. Reason: " + ex.toString(), ex);
}
}
message.setContent (messageMultipart);
}
multipart.addBodyPart(htmlBodyPart);
message.setContent(multipart);
return transportMail(message);
} catch (MessagingException | MailTemplateException ex) {
@ -144,7 +175,6 @@ public class MailService implements Serializable {
String baseUrl = configService.getConfigValue(configKey);
String resetUrlWithToken = baseUrl + "/login.xhtml?token=" + token;
*/
String resetUrlWithToken = passwordResetUrl + "?token=" + token;
model.addParameter("url", baseUrl);
model.addParameter("resetUrl", resetUrlWithToken);