Spring 无法发送 smtp: mail com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 必须先发出 STARTTLS 命令
Spring doesn't work sending smtp: mail com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
我正在尝试从我的 spring 应用程序发送邮件,邮件是用 jhipster 1.1.0 生成的。我的 spring-boot 版本是 1.5.2
我有这个代码:
Application-dev.yml:
spring:
mail:
host: smtp.gmail.com
port: 587
username: myuser@gmail.com
password: mypass
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
#ssl.trust: smtp.gmail.com
starttls.required: true
jhipster:
mail: # specific JHipster mail property, for standard properties see MailProperties
from: myuser@gmail.com
base-url: http://127.0.0.1:8080
MailService.java
package com.pfg.easyschedule.service;
import com.pfg.easyschedule.domain.User;
import io.github.jhipster.config.JHipsterProperties;
import org.apache.commons.lang3.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.MessageSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;
import javax.inject.Inject;
import javax.mail.internet.MimeMessage;
import java.util.Locale;
import java.util.Map;
/**
* Service for sending e-mails.
* <p>
* We use the @Async annotation to send e-mails asynchronously.
* </p>
*/
@Service
public class MailService {
private final Logger log = LoggerFactory.getLogger(MailService.class);
private static final String USER = "user";
private static final String BASE_URL = "baseUrl";
private final JHipsterProperties jHipsterProperties;
private final JavaMailSender javaMailSender;
private final MessageSource messageSource;
private final SpringTemplateEngine templateEngine;
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
MessageSource messageSource, SpringTemplateEngine templateEngine) {
this.jHipsterProperties = jHipsterProperties;
this.javaMailSender = javaMailSender;
this.messageSource = messageSource;
this.templateEngine = templateEngine;
}
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent e-mail to User '{}'", to);
} catch (Exception e) {
log.warn("E-mail could not be sent to user '{}'", to, e);
}
}
@Async
public void sendActivationEmail(User user) {
log.debug("Sending activation e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("activationEmail", context);
String subject = messageSource.getMessage("email.activation.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
@Async
public void sendCreationEmail(User user) {
log.debug("Sending creation e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("creationEmail", context);
String subject = messageSource.getMessage("email.activation.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
@Async
public void sendPasswordResetMail(User user) {
log.debug("Sending password reset e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("passwordResetEmail", context);
String subject = messageSource.getMessage("email.reset.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
}
最后我从控制器使用这个来发送邮件。
Controller.java
@RestController
@RequestMapping("/api")
public class MyClassResource{
private final Logger log =
LoggerFactory.getLogger(MyClassResource.class);
@Autowired
EntityManager entityManager;
@Autowired
MailService mailService;
@PostMapping ("/myurl")
@Timed
public ResponseEntity<Boolean> mymethod(@RequestBody myObject data) {
mailService.sendEmail(
"emailTo@gmail.com",
"message from spring",
"hi world. this message is from spring",
true,
true
);
}
最后我得到这个错误:
c....MailService : E-mail could not be sent to user 'emailTo@gmail.com'
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. w125sm9354452wmb.45 - gsmtp
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:474)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
.........
我一直在阅读有关此错误的信息,并且我在配置中添加了一些东西,就像在应用程序-dev.yml 中一样,但是这个不起作用。我试图从 gmail 获取一个密码应用程序,因为我读到它是必要的,但我不能,因为我没有通过 2 个步骤进行身份验证,最后我更改为接受来自安全性较低的应用程序的 conexion,但都没有成功。不知道为什么不行。
谢谢大家的帮助。
最后,我使用了@Gaël 推荐,现在可以使用了。这是我的新配置
Application-dev.yml:
mail:
host: smtp.gmail.com
port: 587
username: myuser@gmail.com
password: mypass
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
ssl.trust: smtp.gmail.com
jhipster:
mail: # specific JHipster mail property, for standard properties see MailProperties
from: myuser@gmail.com
base-url: http://127.0.0.1:8080
我正在尝试从我的 spring 应用程序发送邮件,邮件是用 jhipster 1.1.0 生成的。我的 spring-boot 版本是 1.5.2
我有这个代码:
Application-dev.yml:
spring:
mail:
host: smtp.gmail.com
port: 587
username: myuser@gmail.com
password: mypass
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
#ssl.trust: smtp.gmail.com
starttls.required: true
jhipster:
mail: # specific JHipster mail property, for standard properties see MailProperties
from: myuser@gmail.com
base-url: http://127.0.0.1:8080
MailService.java
package com.pfg.easyschedule.service;
import com.pfg.easyschedule.domain.User;
import io.github.jhipster.config.JHipsterProperties;
import org.apache.commons.lang3.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.MessageSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;
import javax.inject.Inject;
import javax.mail.internet.MimeMessage;
import java.util.Locale;
import java.util.Map;
/**
* Service for sending e-mails.
* <p>
* We use the @Async annotation to send e-mails asynchronously.
* </p>
*/
@Service
public class MailService {
private final Logger log = LoggerFactory.getLogger(MailService.class);
private static final String USER = "user";
private static final String BASE_URL = "baseUrl";
private final JHipsterProperties jHipsterProperties;
private final JavaMailSender javaMailSender;
private final MessageSource messageSource;
private final SpringTemplateEngine templateEngine;
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
MessageSource messageSource, SpringTemplateEngine templateEngine) {
this.jHipsterProperties = jHipsterProperties;
this.javaMailSender = javaMailSender;
this.messageSource = messageSource;
this.templateEngine = templateEngine;
}
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent e-mail to User '{}'", to);
} catch (Exception e) {
log.warn("E-mail could not be sent to user '{}'", to, e);
}
}
@Async
public void sendActivationEmail(User user) {
log.debug("Sending activation e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("activationEmail", context);
String subject = messageSource.getMessage("email.activation.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
@Async
public void sendCreationEmail(User user) {
log.debug("Sending creation e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("creationEmail", context);
String subject = messageSource.getMessage("email.activation.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
@Async
public void sendPasswordResetMail(User user) {
log.debug("Sending password reset e-mail to '{}'", user.getEmail());
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process("passwordResetEmail", context);
String subject = messageSource.getMessage("email.reset.title", null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
}
}
最后我从控制器使用这个来发送邮件。
Controller.java
@RestController
@RequestMapping("/api") public class MyClassResource{
private final Logger log =
LoggerFactory.getLogger(MyClassResource.class);
@Autowired
EntityManager entityManager;
@Autowired
MailService mailService;
@PostMapping ("/myurl")
@Timed
public ResponseEntity<Boolean> mymethod(@RequestBody myObject data) {
mailService.sendEmail(
"emailTo@gmail.com",
"message from spring",
"hi world. this message is from spring",
true,
true
);
}
最后我得到这个错误:
c....MailService : E-mail could not be sent to user 'emailTo@gmail.com'
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. w125sm9354452wmb.45 - gsmtp
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:474)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
.........
我一直在阅读有关此错误的信息,并且我在配置中添加了一些东西,就像在应用程序-dev.yml 中一样,但是这个不起作用。我试图从 gmail 获取一个密码应用程序,因为我读到它是必要的,但我不能,因为我没有通过 2 个步骤进行身份验证,最后我更改为接受来自安全性较低的应用程序的 conexion,但都没有成功。不知道为什么不行。
谢谢大家的帮助。
最后,我使用了@Gaël 推荐,现在可以使用了。这是我的新配置
Application-dev.yml:
mail:
host: smtp.gmail.com
port: 587
username: myuser@gmail.com
password: mypass
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
ssl.trust: smtp.gmail.com
jhipster:
mail: # specific JHipster mail property, for standard properties see MailProperties
from: myuser@gmail.com
base-url: http://127.0.0.1:8080