如何解决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
中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。