【发布时间】:2017-09-20 02:22:57
【问题描述】:
我想使用 gmail smtp 服务器和 spring 邮件发件人发送电子邮件,它不会抛出任何异常但我的邮件没有发送我没有收到它。
Service 的外观如下:
@Service
public class MailSenderService {
@Autowired
public MailSender mailSender;
public void prepareAndSend(String recipient, String message) {
try {
SimpleMailMessage mail = new SimpleMailMessage();
String from = "testSender1@gmail.com";
String to = "testRecipient1@gmail.com";
String subject = "Test subject";
mail.setFrom(from);
mail.setTo(recipient);
mail.setSubject(subject);
mail.setText(message);
mailSender.send(mail);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
应用程序属性
spring.mail.host = smtp.gmail.com
spring.mail.username = ********@gmail.com
spring.mail.password = ********
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable = true
和pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
我在端口 8080 上部署我的应用程序并使用这两个参数调用此服务,没有捕获到异常但在我的 testRecipient1 收件箱中我没有找到任何新邮件,我错过了什么?
【问题讨论】:
-
您是否将您的发件人电子邮件帐户配置为allow less secure apps as mentioned here?
-
好吧,除了你应该使用记录器来记录错误而不是使用 printStackTrace() 方法之外,你做了一些调试吗?如果没有,你怎么能说一切顺利?
标签: email spring-boot gmail