目录
功能阐述
前序步骤
(1)到QQ邮箱中获取到授权码
代码实现
坑
今天在写一个修改密码的功能的时候要用到邮箱的发送,然后因为这个项目比较老旧了,采用的是javaWeb和jsp的配置,对于我只使用过springBoot整合的javaMail的我来说确实值得纪念一下这次的问题。
功能阐述
实现一个方法,让这个方法可以给对应的邮箱发送邮件,我们这里指定只能为QQ邮箱了。
前序步骤
(1)到QQ邮箱中获取到授权码
目前最新版本2024年5月89号是这样的步骤找到
这里点击生成授权码就可以获取到你授权码了。
代码实现
package com.example.util;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class emailSend {
public void sendEmail(String recipientEmail, String subject, String messageText) throws MessagingException {
// 邮箱账号
final String username = "你要发送邮件的QQ号@qq.com";
// 邮箱授权码
final String password = "aaaccc到QQ邮箱中申请到的授权码,不是QQ密码";
// 设置邮件服务器的属性
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.qq.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// 这里就有一个坑
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
// 创建认证对象并传入用户名和密码信息
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// 创建邮件信息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
message.setSubject(subject);
message.setText(messageText);
// 发送邮件
Transport.send(message);
}
public static void main(String[] args) throws MessagingException {
emailSend emailSend = new emailSend();
emailSend.sendEmail("目的邮箱@qq.com", "test", "test");
}
}
坑
如果你的代码中没有添加下面这行代码的话会报错
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
D:\JDK8\bin\java.exe "-javaagent:D:\IDEA\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=28265:D:\IDEA\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath D:\JDK8\jre\lib\charsets.jar;D:\JDK8\jre\lib\deploy.jar;D:\JDK8\jre\lib\ext\access-bridge-64.jar;D:\JDK8\jre\lib\ext\cldrdata.jar;D:\JDK8\jre\lib\ext\dnsns.jar;D:\JDK8\jre\lib\ext\jaccess.jar;D:\JDK8\jre\lib\ext\jfxrt.jar;D:\JDK8\jre\lib\ext\localedata.jar;D:\JDK8\jre\lib\ext\nashorn.jar;D:\JDK8\jre\lib\ext\sunec.jar;D:\JDK8\jre\lib\ext\sunjce_provider.jar;D:\JDK8\jre\lib\ext\sunmscapi.jar;D:\JDK8\jre\lib\ext\sunpkcs11.jar;D:\JDK8\jre\lib\ext\zipfs.jar;D:\JDK8\jre\lib\javaws.jar;D:\JDK8\jre\lib\jce.jar;D:\JDK8\jre\lib\jfr.jar;D:\JDK8\jre\lib\jfxswt.jar;D:\JDK8\jre\lib\jsse.jar;D:\JDK8\jre\lib\management-agent.jar;D:\JDK8\jre\lib\plugin.jar;D:\JDK8\jre\lib\resources.jar;D:\JDK8\jre\lib\rt.jar;D:\codeTemp\GWJshoppingCar\target\classes;D:\codeTemp\GWJshoppingCar\src\main\webapp\WEB-INF\lib\jstl-1.2.jar;D:\codeTemp\GWJshoppingCar\src\main\webapp\WEB-INF\lib\standard-1.1.2.jar;D:\codeTemp\GWJshoppingCar\src\main\webapp\WEB-INF\lib\mysql-connector-java-8.0.26.jar;D:\Maven\repository\com\github\penggle\kaptcha\2.3.2\kaptcha-2.3.2.jar;D:\Maven\repository\com\jhlabs\filters\2.0.235-1\filters-2.0.235-1.jar;D:\Maven\repository\javax\mail\mail\1.4.7\mail-1.4.7.jar;D:\Maven\repository\javax\activation\activation\1.1\activation-1.1.jar com.example.util.emailSend
Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.example.util.emailSend.sendEmail(emailSend.java:40)
at com.example.util.emailSend.main(emailSend.java:46)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:106)
at sun.security.ssl.TransportContext.kickstart(TransportContext.java:238)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:410)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:389)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 8 moreProcess finished with exit code 1
注意看标红的字段,TLS有问题。这就是版本的问题了,现在的安全校验都很严格,一定要制定这里TLS(以前是不用指定的)。