Commit 02218294 authored by Simon Reis's avatar Simon Reis
Browse files

Added htmlMail-Feature

parent de1fd0d0
...@@ -33,4 +33,9 @@ public class EmailConfig { ...@@ -33,4 +33,9 @@ public class EmailConfig {
@Value("${email.port:1025}") @Value("${email.port:1025}")
private String emailPort; private String emailPort;
@Value("${email.isHtmlEmail:true}")
private boolean isHtmlEmail;
@Value("${email.isUseHtmlEmailTemplate:true}")
private boolean isUseHtmlEmailTemplate;
} }
...@@ -24,7 +24,11 @@ import javax.mail.internet.InternetAddress; ...@@ -24,7 +24,11 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import java.util.*; import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -42,18 +46,24 @@ public class Email { ...@@ -42,18 +46,24 @@ public class Email {
private final Properties props = new Properties(); private final Properties props = new Properties();
private final MimeMultipart multipart = new MimeMultipart(); private final MimeMultipart multipart = new MimeMultipart();
private final MimeMessage message; private final MimeMessage message;
private final boolean htmlMail;
public Email(String smtpHost, String port) { public Email(String smtpHost, String port, boolean htmlMail) {
props.put("mail.smtp.auth", "false"); props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", port); props.put("mail.smtp.port", port);
this.htmlMail = htmlMail;
Session session = Session.getInstance(props); Session session = Session.getInstance(props);
message = new MimeMessage(session); message = new MimeMessage(session);
} }
public void addText(String text) throws MessagingException { public void addText(String text) throws MessagingException {
MimeBodyPart textBody = new MimeBodyPart(); MimeBodyPart textBody = new MimeBodyPart();
if (htmlMail) {
textBody.setText(text, MAIL_ENCODING, "html");
} else {
textBody.setText(text, MAIL_ENCODING); textBody.setText(text, MAIL_ENCODING);
}
textBody.setDisposition(INLINE); textBody.setDisposition(INLINE);
multipart.addBodyPart(textBody); multipart.addBodyPart(textBody);
} }
......
...@@ -29,7 +29,7 @@ public class GfiEmail { ...@@ -29,7 +29,7 @@ public class GfiEmail {
protected String emailBody; protected String emailBody;
public GfiEmail(EmailConfig emailConfig) throws MessagingException { public GfiEmail(EmailConfig emailConfig) throws MessagingException {
email = new Email(emailConfig.getSmtpHost(), emailConfig.getEmailPort()); email = new Email(emailConfig.getSmtpHost(), emailConfig.getEmailPort(), emailConfig.isHtmlEmail());
email.setFrom(emailConfig.getSender()); email.setFrom(emailConfig.getSender());
} }
......
...@@ -18,10 +18,13 @@ package org.eclipse.openk.gridfailureinformation.mailexport.service; ...@@ -18,10 +18,13 @@ package org.eclipse.openk.gridfailureinformation.mailexport.service;
import org.eclipse.openk.gridfailureinformation.mailexport.config.EmailConfig; import org.eclipse.openk.gridfailureinformation.mailexport.config.EmailConfig;
import org.eclipse.openk.gridfailureinformation.mailexport.dtos.MailMessageDto; import org.eclipse.openk.gridfailureinformation.mailexport.dtos.MailMessageDto;
import org.eclipse.openk.gridfailureinformation.mailexport.email.GfiEmail; import org.eclipse.openk.gridfailureinformation.mailexport.email.GfiEmail;
import org.eclipse.openk.gridfailureinformation.mailexport.util.ResourceLoaderBase;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -29,12 +32,19 @@ import java.util.List; ...@@ -29,12 +32,19 @@ import java.util.List;
@Service @Service
public class EmailService { public class EmailService {
public static final String DATE_PATTERN = "dd.MM.yyyy, HH:mm:ss";
public static final String TEMPLATE_EMAIL_CONTENT_PLACEHOLDER = "##EMAIL_CONTENT_PLACEHOLDER###";
public static final String EMAIL_CREATION_SENT_DATE_PLACEHOLDER = "##CREATION_SENT_DATE###";
public static final String EMAIL_TEMPLATE_PATH = "emails/emailTemplate.html";
@Autowired @Autowired
EmailConfig emailConfig; EmailConfig emailConfig;
public void sendMail(MailMessageDto mailMessageDto) throws MessagingException { public void sendMail(MailMessageDto mailMessageDto) throws MessagingException {
GfiEmail gfiEmail = new GfiEmail(emailConfig); GfiEmail gfiEmail = new GfiEmail(emailConfig);
gfiEmail.setEmailContent(mailMessageDto.getEmailSubject(), mailMessageDto.getBody()); gfiEmail.setEmailContent(mailMessageDto.getEmailSubject(), useHtmlEmailTemplate(mailMessageDto.getBody()));
gfiEmail.setRecipientsTo(mailMessageDto.getMailAddresses()); gfiEmail.setRecipientsTo(mailMessageDto.getMailAddresses());
gfiEmail.sendEmail(); gfiEmail.sendEmail();
} }
...@@ -43,9 +53,23 @@ public class EmailService { ...@@ -43,9 +53,23 @@ public class EmailService {
GfiEmail gfiEmail = new GfiEmail(emailConfig); GfiEmail gfiEmail = new GfiEmail(emailConfig);
List<String> mailAddressList = new ArrayList<>(); List<String> mailAddressList = new ArrayList<>();
mailAddressList.add(recipient); mailAddressList.add(recipient);
gfiEmail.setEmailContent("Test Betreff", "Email Text Inhalt Test"); gfiEmail.setEmailContent("Test Betreff", useHtmlEmailTemplate("Email Text Inhalt Test"));
gfiEmail.setRecipientsTo(mailAddressList); gfiEmail.setRecipientsTo(mailAddressList);
gfiEmail.sendEmail(); gfiEmail.sendEmail();
} }
private String useHtmlEmailTemplate(String emailText) {
if (!emailConfig.isHtmlEmail() || !emailConfig.isUseHtmlEmailTemplate()) {
return emailText;
}
String htmlEmailTemplate = new ResourceLoaderBase().loadStringFromResource(EMAIL_TEMPLATE_PATH);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
String nowDateString = LocalDateTime.now().format(dateTimeFormatter);
htmlEmailTemplate = htmlEmailTemplate.replace(EMAIL_CREATION_SENT_DATE_PLACEHOLDER, nowDateString);
return htmlEmailTemplate.replace(TEMPLATE_EMAIL_CONTENT_PLACEHOLDER, emailText);
}
} }
/*
*******************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.mailexport.util;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Log4j2
public class ResourceLoaderBase {
private String stream2String(InputStream is, String filename) {
StringWriter writer = new StringWriter();
BOMInputStream bomInputStream = new BOMInputStream(is, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE,
ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
try {
IOUtils.copy(bomInputStream, writer, StandardCharsets.UTF_8.name());
} catch (IOException e) {
log.error("Fehler in stream2String()", e);
return "";
}
log.debug("Datei erfolgreich eingelesen: " + filename);
return writer.toString();
}
public String loadStringFromResource(String filename) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(filename);
if (inputStream==null){
log.error("Datei nicht gefunden: " + filename);
return null;
}
log.debug("Datei existiert: " + filename);
try {
URL resource = classLoader.getResource(filename);
if (resource != null) {
URI uri = resource.toURI();
log.debug("Uniform Resource Identifier (URI): " + uri);
}
} catch (URISyntaxException e) {
log.error("Fehler in loadStringFromResource: " + filename, e);
}
return stream2String(inputStream, filename);
}
public String loadFromPath(String path) {
try {
Path paths = Paths.get(path);
log.debug("paths: " + path);
try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
return stream2String(inputStream, paths.getFileName().toString());
}
} catch (IOException e) {
log.error("Fehler in loadFromPath", e);
return null;
}
}
}
...@@ -27,6 +27,8 @@ email: ...@@ -27,6 +27,8 @@ email:
sender: sender@test.tester sender: sender@test.tester
smtpHost: entdockergss smtpHost: entdockergss
port: 1025 port: 1025
isHtmlEmail: true
isUseHtmlEmailTemplate: true
security.endpoint: security.endpoint:
user: ${GFI_MANUAL_ENDPOINTS_USERNAME} user: ${GFI_MANUAL_ENDPOINTS_USERNAME}
......
...@@ -39,6 +39,8 @@ email: ...@@ -39,6 +39,8 @@ email:
sender: sender@olaf.senderson sender: sender@olaf.senderson
smtpHost: entdockergss smtpHost: entdockergss
port: 1025 port: 1025
isHtmlEmail: true
isUseHtmlEmailTemplate: true
security.endpoint: security.endpoint:
user: ${GFI_MANUAL_ENDPOINTS_USERNAME} user: ${GFI_MANUAL_ENDPOINTS_USERNAME}
......
<!--
*******************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
-->
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>open KONSEQUENZ Email</title>
<style>
@media only screen and (max-width: 580px) {
table[class=body] h1 {
font-size: 28px !important;
margin-bottom: 10px !important;
}
table[class=body] p,
table[class=body] ul,
table[class=body] ol,
table[class=body] td,
table[class=body] span,
table[class=body] a {
font-size: 16px !important;
}
table[class=body] .wrapper,
table[class=body] .article {
padding: 10px !important;
}
table[class=body] .content {
padding: 0 !important;
}
table[class=body] .container {
padding: 0 !important;
width: 100% !important;
}
table[class=body] .main {
border-left-width: 0 !important;
border-radius: 0 !important;
border-right-width: 0 !important;
}
table[class=body] .btn table {
width: 100% !important;
}
table[class=body] .btn a {
width: 100% !important;
}
table[class=body] .img-responsive {
height: auto !important;
max-width: 100% !important;
width: auto !important;
}
}
@media all {
.ExternalClass {
width: 100%;
}
.ExternalClass,
.ExternalClass p,
.ExternalClass span,
.ExternalClass font,
.ExternalClass td,
.ExternalClass div {
line-height: 100%;
}
.apple-link a {
color: inherit !important;
font-family: inherit !important;
font-size: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
text-decoration: none !important;
}
#MessageViewBody a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}
.btn-primary table td:hover {
background-color: #286090 !important;
}
.btn-primary a:hover {
background-color: #286090 !important;
border-color: #204d74 !important;
}
}
</style>
</head>
<body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
<span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
<table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
<td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 680px; padding: 10px; width: 680px;">
<div class="content" style="overflow: auto; position: inherit; box-sizing: border-box; display: block; Margin: 0 auto; max-width: 680px; padding: 10px;">
<!-- START CENTERED WHITE CONTAINER -->
<table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">
<div style="padding: 3px; border: 0; background: rgb(232, 238, 231); background: -moz-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(232, 238, 231, 1)), color-stop(75%, rgba(229, 237, 242, 1))); background: -webkit-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -o-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -ms-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: linear-gradient(to right, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#e8eee7', endColorstr='#e5edf2', GradientType=1);">
<span style="margin-left: 6px; font-size: 2em; color: #000; font-weight: 500; text-transform: lowercase;">Open</span>
<span style="margin-left: 3px; font-size: 1.5em; color: #000; text-transform: uppercase; font-weight: 300;">Konsequenz</span>
</div>
<div style="position: relative; height: 4px; width: 100%; background: rgb(121, 182, 28); background: -moz-linear-gradient(left, rgba(121, 182, 28, 1) 0%, rgba(2, 129, 196, 1) 75%); background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(121, 182, 28, 1)), color-stop(75%, rgba(2, 129, 196, 1))); background: -webkit-linear-gradient(left, rgba(121, 182, 28, 1) 0%, rgba(2, 129, 196, 1) 75%); background: -o-linear-gradient(left, rgba(121, 182, 28, 1) 0%, rgba(2, 129, 196, 1) 75%); background: -ms-linear-gradient(left, rgba(121, 182, 28, 1) 0%, rgba(2, 129, 196, 1) 75%); background: linear-gradient(to right, rgba(121, 182, 28, 1) 0%, rgba(2, 129, 196, 1) 75%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#79b61c', endColorstr='#0281c4', GradientType=1);"></div>
<div style="border: 0; background: rgb(232, 238, 231); background: -moz-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(232, 238, 231, 1)), color-stop(75%, rgba(229, 237, 242, 1))); background: -webkit-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -o-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: -ms-linear-gradient(left, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); background: linear-gradient(to right, rgba(232, 238, 231, 1) 0%, rgba(229, 237, 242, 1) 75%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#e8eee7', endColorstr='#e5edf2', GradientType=1); padding: 6px 20px;">
<span style="font-size: 2.2em; color: #000; font-weight: 300;">Störungsinformationstool</span>
</div>
<!-- START MAIN CONTENT AREA -->
<tr>
<td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
##EMAIL_CONTENT_PLACEHOLDER###
</td>
</tr>
</table>
</td>
</tr>
<!-- END MAIN CONTENT AREA -->
</table>
<!-- START FOOTER -->
<div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
<tr>
<td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
<span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">Automatisierte Mail wurde erstellt und versendet am:</span>
<br> ##CREATION_SENT_DATE###
</td>
</tr>
<tr>
<td class="content-block powered-by" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
Powered by <a href="https://www.openkonsequenz.de/" style="color: #999999; font-size: 12px; text-align: center; text-decoration: none;">openKONSEQUENZ</a>.
</td>
</tr>
</table>
</div>
<!-- END FOOTER -->
<!-- END CENTERED WHITE CONTAINER -->
</div>
</td>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment