Затем я добавил следующую конфигурацию в свой application.yml:
Код: Выделить всё
mail:
host: smtp.gmail.com
port: 587
username: MY_EMAIL
password: MY_PASSWORD
properties:
mail:
smtp:
trust: "*"
auth: true
starttls:
enabled: true
required: true
connectiontimeout: 5000
timeout: 3000
writetimeout: 5000
Код: Выделить всё
@Service
@RequiredArgsConstructor
public class EmailService {
@Value("${spring.mail.username}")
private String mailOrigin;
private final JavaMailSender mailSender;
private final SpringTemplateEngine templateEngine;
@Async
public void sendEmail(String to, String userName, EmailTemplateName emailTemplate, String confirmationUrl, String activationCode, String subject) throws MessagingException{
String templateName;
if(emailTemplate == null){
templateName = "confirm-email";
}else{
templateName = emailTemplate.getName();
}
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, MimeMessageHelper.MULTIPART_MODE_MIXED, StandardCharsets.UTF_8.name());
Map properties = new HashMap();
properties.put("username", userName);
properties.put("confirmationUrl", confirmationUrl);
properties.put("activation_code", activationCode);
properties.put("mail.smtp.starttls.enable", "true");
Context context = new Context();
context.setVariables(properties);
helper.setFrom(mailOrigin);
helper.setTo(to);
helper.setSubject(subject);
String template = templateEngine.process(templateName, context);
helper.setText(template, true);
mailSender.send(mimeMessage);
}
}
Код: Выделить всё
org.springframework.mail.MailSendException: Failed messages:
org.eclipse.angus.mail.smtp.SMTPSendFailedException: 530-5.7.0 Must issue a STARTTLS
command first. For more information, go to
530-5.7.0 https://support.google.com/a/answer/3221692 and review RFC 3207
530 5.7.0 specifications. 5b1f17b1804b1-432da24498csm101062995e9.1 - gsmtp
Подробнее здесь: https://stackoverflow.com/questions/792 ... end-emails