Commit 54b76803 authored by Matija Obreza's avatar Matija Obreza
Browse files

SMTP: Mail sender is null when mail.host is blank

- Log outgoing message using messagePreparator
parent 35a1a5ff
......@@ -17,6 +17,7 @@ package org.gringlobal.application.config;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -54,6 +55,10 @@ public class MailConfig {
@Bean
public JavaMailSenderImpl mailSender() {
if (StringUtils.isBlank(host)) {
System.err.println("SMTP is not configured");
return null;
}
final JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(host);
sender.setPort(port);
......
......@@ -15,9 +15,11 @@
*/
package org.gringlobal.service.impl;
import java.util.Arrays;
import java.io.ByteArrayOutputStream;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.service.EMailService;
import org.slf4j.Logger;
......@@ -36,7 +38,7 @@ public class EMailServiceImpl implements EMailService {
private static final Logger LOG = LoggerFactory.getLogger(EMailServiceImpl.class);
@Autowired
@Autowired(required = false)
private JavaMailSender mailSender;
@Autowired
......@@ -74,14 +76,12 @@ public class EMailServiceImpl implements EMailService {
}
public void sendSimpleEmail(final String subject, final String text, final String emailFrom, final String[] emailCc, final String... emailTo) {
printDebugInfo(subject, text, emailFrom, emailTo);
final MimeMessagePreparator preparator = mimeMessage -> {
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setFrom(emailFrom);
message.setTo(emailTo);
LOG.info("Email to: {}", ArrayUtils.toString(emailTo, "<NULL>"));
if (emailCc != null && emailCc.length > 0) {
message.setCc(emailCc);
}
......@@ -90,14 +90,31 @@ public class EMailServiceImpl implements EMailService {
message.setText(text, true);
};
if (LOG.isInfoEnabled()) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
var message = new MimeMessage((Session)null);
preparator.prepare(message);
message.writeTo(baos);
LOG.info("Outgoing message:\n{}\n<EOF>", baos);
} catch (Exception e) {
LOG.info(e.getMessage(), e);
}
}
doSend(preparator);
}
protected void doSend(final MimeMessagePreparator preparator) {
// execute sender in separate thread
if (mailSender == null) {
LOG.warn("SMTP not enabled, email was not sent.");
return;
}
JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
if (async) {
// execute sender in separate thread
LOG.info("Sending email message asynchroniously");
executor.submit(() -> {
try {
......@@ -114,11 +131,4 @@ public class EMailServiceImpl implements EMailService {
}
}
protected void printDebugInfo(String subject, String text, String emailFrom, String[] emailTo) {
System.out.println(getDebugString(subject, text, emailFrom, emailTo));
}
protected String getDebugString(String subject, String text, String emailFrom, String[] emailTo) {
return String.format(debugMessage, emailFrom, Arrays.toString(emailTo), subject, text);
}
}
Supports Markdown
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