作者:我要公道2602932023 | 来源:互联网 | 2023-09-25 18:03
问题描述DEBUG:JavaMailversion1.6.1DEBUG:successfullyloadedresource:META-INFjavamail.default.
问题描述
DEBUG: JavaMail version 1.6.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.163.com", port 465, isSSL false
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: could not connect to host "smtp.163.com", port: 465, response: -1
2021-06-02 16:35:42.323 ERROR 11141 --- [nio-8999-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1] with root causejavax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2197) ~[javax.mail-1.6.1.jar:1.6.1]at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740) ~[javax.mail-1.6.1.jar:1.6.1]at javax.mail.Service.connect(Service.java:366) ~[javax.mail-1.6.1.jar:1.6.1]at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:515) ~[spring-context-support-5.0.8.RELEASE.jar:5.0.8.RELEASE]at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:435) ~[spring-context-support-5.0.8.RELEASE.jar:5.0.8.RELEASE]at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:321) ~[spring-context-support-5.0.8.RELEASE.jar:5.0.8.RELEASE]
项目配置
spring:mail:host: smtp.163.comusername: cocoococ@163.compassword: ajhdasdkaklsjdlport: 465default-encoding: UTF-8properties:mail:smtp:socketFactoryClass: javax.net.ssl.SSLSocketFactorydebug: true
发送代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;@RestController
@RequestMapping("test")
public class TestController {@AutowiredJavaMailSender javaMailSender;@GetMapping("sendMail")public void sendMail(){SimpleMailMessage message = new SimpleMailMessage();message.setSubject("这是一封测试邮件");message.setFrom("cocoococ@163.com");message.setTo("222qwioqin@qq.com");message.setSentDate(new Date());message.setText("这是测试邮件的正文");javaMailSender.send(message);}}
问题调试
可疑点在于上面红线位置
- 我明明设置了账号、授权码登录,但是useAuth还是false
- isSsl 也是false
从报错堆栈中找到
com.sun.mail.smtp.SMTPTransport#protocolConnect
就是说 useAuth 其实是从属性中解析的,这里的name代表协议类型(smtp、pop3)
那么完整的属性名应该是 mail.smtp.auth
接着
com.sun.mail.smtp.SMTPTransport#openServer(java.lang.String, int)
这里的isSSL
是成员属性
根据调试只有在构造函数中才会赋值
com.sun.mail.smtp.SMTPTransport#SMTPTransport(javax.mail.Session, javax.mail.URLName, java.lang.String, boolean)
那么完整的属性名应该是 mail.smtp.ssl.enable
所以只要修改yml文件即可
解决方案
spring:mail:host: smtp.163.comusername: cocoococ@163.compassword: ajhdasdkaklsjdlport: 465default-encoding: UTF-8properties:mail:smtp:starttls:enable: trueauth: truessl:enable: truesocketFactoryClass: javax.net.ssl.SSLSocketFactorydebug: true
server:port: 8999servlet:path: /mall