JavaMail Spring引导属性无法识别属性

问题描述

我有一个带有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:连接被拒绝:connect;消息异常(1)是: 失败的消息1:com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1; 嵌套的异常是: java.net.ConnectException:连接被拒绝:connect

有什么主意吗?

解决方法

默认情况下,spring.properties不是属性源,请尝试将它们放在application.properties中。