|
|
@ -3,6 +3,7 @@ package ru.ulstu.user.service;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.boot.autoconfigure.mail.MailProperties;
|
|
|
|
import org.springframework.boot.autoconfigure.mail.MailProperties;
|
|
|
|
|
|
|
|
import org.springframework.mail.MailException;
|
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
@ -13,6 +14,7 @@ import ru.ulstu.configuration.ApplicationProperties;
|
|
|
|
import ru.ulstu.configuration.Constants;
|
|
|
|
import ru.ulstu.configuration.Constants;
|
|
|
|
import ru.ulstu.user.model.User;
|
|
|
|
import ru.ulstu.user.model.User;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
@ -38,10 +40,9 @@ public class MailService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
@Async
|
|
|
|
public void sendEmail(String to, String subject, String content) {
|
|
|
|
public void sendEmail(String to, String subject, String content) throws MessagingException {
|
|
|
|
log.debug("Send email to '{}' with subject '{}'", to, subject);
|
|
|
|
log.debug("Send email to '{}' with subject '{}'", to, subject);
|
|
|
|
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
|
|
|
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
|
|
|
try {
|
|
|
|
|
|
|
|
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name());
|
|
|
|
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name());
|
|
|
|
message.setTo(to);
|
|
|
|
message.setTo(to);
|
|
|
|
message.setFrom(mailProperties.getUsername());
|
|
|
|
message.setFrom(mailProperties.getUsername());
|
|
|
@ -49,13 +50,6 @@ public class MailService {
|
|
|
|
message.setText(content, true);
|
|
|
|
message.setText(content, true);
|
|
|
|
javaMailSender.send(mimeMessage);
|
|
|
|
javaMailSender.send(mimeMessage);
|
|
|
|
log.debug("Sent email to User '{}'", to);
|
|
|
|
log.debug("Sent email to User '{}'", to);
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}'", to, e);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
@Async
|
|
|
@ -64,7 +58,17 @@ public class MailService {
|
|
|
|
context.setVariable(USER, user);
|
|
|
|
context.setVariable(USER, user);
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
|
String content = templateEngine.process(templateName, context);
|
|
|
|
String content = templateEngine.process(templateName, context);
|
|
|
|
|
|
|
|
try {
|
|
|
|
sendEmail(user.getEmail(), subject, content);
|
|
|
|
sendEmail(user.getEmail(), subject, content);
|
|
|
|
|
|
|
|
} catch (
|
|
|
|
|
|
|
|
Exception e) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}'", user.getEmail(), e);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Todo: выделить сервис нотификаций
|
|
|
|
//Todo: выделить сервис нотификаций
|
|
|
@ -75,11 +79,21 @@ public class MailService {
|
|
|
|
context.setVariable(USER, user);
|
|
|
|
context.setVariable(USER, user);
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
|
String content = templateEngine.process(templateName, context);
|
|
|
|
String content = templateEngine.process(templateName, context);
|
|
|
|
|
|
|
|
try {
|
|
|
|
sendEmail(user.getEmail(), subject, content);
|
|
|
|
sendEmail(user.getEmail(), subject, content);
|
|
|
|
|
|
|
|
} catch (
|
|
|
|
|
|
|
|
Exception e) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}'", user.getEmail(), e);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
@Async
|
|
|
|
public void sendEmailFromTemplate(Map<String, Object> variables, String templateName, String subject, String email) {
|
|
|
|
public void sendEmailFromTemplate(Map<String, Object> variables, String templateName, String subject, String email)
|
|
|
|
|
|
|
|
throws MessagingException {
|
|
|
|
Context context = new Context();
|
|
|
|
Context context = new Context();
|
|
|
|
variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue()));
|
|
|
|
variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue()));
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
|
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
|
|
@ -97,8 +111,7 @@ public class MailService {
|
|
|
|
sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET);
|
|
|
|
sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
public void sendInviteMail(Map<String, Object> variables, String email) throws MessagingException {
|
|
|
|
public void sendInviteMail(Map<String, Object> variables, String email) {
|
|
|
|
|
|
|
|
sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email);
|
|
|
|
sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|