我正在使用以下配置发送电子邮件(MimeMessagePreparator)。
- spring.mail.default-encoding=UTF-8 spring.mail.host=smtp.sendgrid.net
spring.mail.username=SEND-GRID-USER-NAME
spring.mail.password=SEND-GRID-PASSWORD spring.mail.port=587
spring.mail.protocol=smtp spring.mail.test-connection=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
但是发送网格不会将电子邮件发送给相应的用户。
@Async
@Override
public Future<Boolean> sendEmailNotification(EmailTemplateDTO emailTemplateDTO) {
boolean flag = false;
if (emailTemplateDTO != null) {
final String emailContent = mailContentBuilder.buildEmailContent(emailTemplateDTO.getObj(),
emailTemplateDTO.getEmailTemplate());
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true,
StandardCharsets.UTF_8.name());
messageHelper.setSubject(emailTemplateDTO.getEmailTemplate().getSubject());
messageHelper.setFrom(fromEmail);
messageHelper.setTo(emailTemplateDTO.getTo().stream().toArray(String[]::new));
messageHelper.setText(emailContent, true);
final Resource fileResource = resourceLoader.getResource("classpath:static/logo.png");
messageHelper.addInline(EmailConstants.XXXX.getName(), fileResource);
};
try {
mailSender.send(messagePreparator);
flag = true;
} catch (MailException e) {
e.printStackTrace();
log.error(e.getLocalizedMessage());
}
return new AsyncResult<>(flag);
}
return new AsyncResult<>(flag);
}