【发布时间】:2020-12-30 18:21:03
【问题描述】:
我有一个带有 javamail 的 Spring Boot 应用程序,我配置了 spring.properties,但它似乎没有使用属性来发送电子邮件。
#mail properties
spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.username = eeee@gmail.com
spring.mail.password = eeee
spring.mail.protocol = smtp
spring.mail.defaultEncoding=UTF-8
spring.mail.smtp.starttls.enable=false
spring.mail.smtp.starttls.required=false
spring.mail.smtp.auth=true
spring.mail.smtp.connectiontimeout=5000
spring.mail.smtp.timeout=5000
spring.mail.smtp.writetimeout=5000
这里是发送电子邮件的 java 代码
@Service
public class MailSenderServiceImpl implements IMailSenderService{
@Autowired
JavaMailSender mailSender;
@Override
public void sendEmailStatusNotification(Shipment shipment) throws IOException, MessagingException{
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setFrom(shipment.getSender().getSenderEmail());
message.setTo(new String[] {shipment.getRecipient().getRecipientEmail()});
message.setSubject("Shipment "+shipment.getTrackingNumber()+"status update");
message.setText("the shipment status is now :"+shipment.getStatus().getLabel());
this.mailSender.send(mimeMessage);
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
这里我们看到了这个错误
org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是 com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1; 嵌套异常是: java.net.ConnectException:连接被拒绝:连接。失败消息:com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1; 嵌套异常是: java.net.ConnectException:连接被拒绝:连接;消息异常 (1) 是: 失败消息 1:com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1; 嵌套异常是: java.net.ConnectException:连接被拒绝:连接
有什么想法吗?
【问题讨论】:
标签: spring-boot jakarta-mail properties-file