springboot javaMailbox SMTP连接错误:无法连接到主机,端口:localhost,25;超时-1;

f0brbegy  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(192)

我设置端口587作为recomended在我的应用程序.propertys,但springboot似乎仍然使用端口25尽管,有人能告诉我为什么端口25不工作,或者更重要的是我如何修复我的smtp端口587?提前感谢!
错误类型:

  1. 2023-12-18T15:30:19.084+01:00 ERROR 15800 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  2. nested exception is:
  3. java.net.ConnectException: Connection refused: connect; message exceptions (1) are:
  4. Failed message 1: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  5. nested exception is:
  6. java.net.ConnectException: Connection refused: connect] with root cause
  7. java.net.ConnectException: Connection refused: connect
  8. at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
  9. at java.base/sun.nio.ch.Net.connect(Net.java:579) ~[na:na]
  10. at java.base/sun.nio.ch.Net.connect(Net.java:568) ~[na:na]
  11. at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:585) ~[na:na]
  12. at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
  13. at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
  14. at java.base/java.net.Socket.connect(Socket.java:583) ~[na:na]
  15. at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:361) ~[jakarta.mail-1.1.0.jar:na]
  16. at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236) ~[jakarta.mail-1.1.0.jar:na]
  17. at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209) ~[jakarta.mail-1.1.0.jar:na]
  18. at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:754) ~[jakarta.mail-1.1.0.jar:na]
  19. at jakarta.mail.Service.connect(Service.java:342) ~[jakarta.mail-1.1.0.jar:na]
  20. at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518) ~[spring-context-support-6.0.12.jar:6.0.12]
  21. at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437) ~[spring-context-support-6.0.12.jar:6.0.12]

字符串
我代码:

  1. @Component
  2. public class EmailSender {
  3. @Bean
  4. public static JavaMailSenderImpl javaMailSender() {
  5. JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
  6. javaMailSender.setProtocol("SMTP");
  7. javaMailSender.setHost("smtp.office365.com");
  8. javaMailSender.setPort(587);
  9. return javaMailSender;
  10. }
  11. JavaMailSender mailSender = new JavaMailSenderImpl();
  12. public void sendEmail(String to, String subject, String message){
  13. SimpleMailMessage mailMessage = new SimpleMailMessage();
  14. mailMessage.setTo(to);
  15. mailMessage.setSubject(subject);
  16. mailMessage.setText(message);
  17. mailSender.send(mailMessage);
  18. }
  19. }


我的应用程序。

  1. # mail
  2. #spring.mail.host=smtp-mail.outlook.com
  3. spring.mail.host=smtp.office365.com
  4. spring.mail.properties.mail.transport.protocol=smtp
  5. spring.mail.port=587
  6. spring.mail.username=${[email protected]}
  7. spring.mail.password=${*secret*}
  8. spring.mail.properties.mail.smtp.auth=true
  9. spring.mail.properties.mail.smtp.starttls.enable=true
  10. spring.mail.debug=true


我试图在我的应用程序中发送电子邮件,但在端口25处出现错误,而我已设置smtp端口587
迎接

vxqlmq5t

vxqlmq5t1#

https://stackoverflow.com/a/38629240/23120849
这是一个很好的工作!它工作。
当然,对于Outlook的端口和主机:
example.put(“mail.smtp.host“,“smtp.office365.com“);
public String s(“String s”);

相关问题