application.properties配置信息
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.password=**********
spring.mail.username=1*****12@qq.com
spring.mail.port=465
spring.mail.properties.mail.stmp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.stmp.socketFactory.port=465
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
发送邮件代码:
`SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("1588974**@163.com");
msg.setFrom("122******471@qq.com");
msg.setSubject("测试");
msg.setSentDate(new Date());
msg.setText("测试邮件");
javaMailSender.send(msg);`
我已经开启了qq的SMTP服务。授权码是正确的。但是还是不能发送邮件
报错信息:
DEBUG: JavaMail version 1.6.2
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 true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: could not connect to host "smtp.qq.com", port: 465, response: -1
题主贴出的配置和代码有两个问题:
1、端口号。根据QQ邮箱的官方说明,端口号可以是465或587,但我测试的时候465是超时的,587就正常。
2、运行贴出的代码尝试发邮件,会报错,报错信息
根据这个信息,host和port这两个参数都没有作为参数传入,运行的时候压根就没有连去QQ邮箱的服务器。
因为弄了一轮都发不出,于是归零重新找资料实现。
下面是我的代码,参考的是spring的官方文档。
配置文件沿用题主的。用了springboot,版本号2.2.3.RELEASE。