javaMail快速部署——发邮件喽~

目录

功能阐述

前序步骤

(1)到QQ邮箱中获取到授权码

代码实现


今天在写一个修改密码的功能的时候要用到邮箱的发送,然后因为这个项目比较老旧了,采用的是javaWeb和jsp的配置,对于我只使用过springBoot整合的javaMail的我来说确实值得纪念一下这次的问题。

功能阐述

实现一个方法,让这个方法可以给对应的邮箱发送邮件,我们这里指定只能为QQ邮箱了。

前序步骤

(1)到QQ邮箱中获取到授权码

目前最新版本2024年5月89号是这样的步骤找到

这里点击生成授权码就可以获取到你授权码了。

代码实现

java 复制代码
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");
    }
}

如果你的代码中没有添加下面这行代码的话会报错

java 复制代码
        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 more

Process finished with exit code 1

注意看标红的字段,TLS有问题。这就是版本的问题了,现在的安全校验都很严格,一定要制定这里TLS(以前是不用指定的)。

相关推荐
刘晓倩1 分钟前
Python3的Sequence
开发语言·python
_院长大人_3 分钟前
Spring Boot 客户端设计示例:自动刷新 Token 并重试接口调用(Springboot Starter 封装)
java·spring boot·后端
卷到起飞的数分9 分钟前
19.Spring Boot原理1
java·spring boot·后端
消失的旧时光-194311 分钟前
彻底理解 synchronized:实例锁、类锁与自定义锁的原理和最佳实践
java·开发语言
鹿里噜哩12 分钟前
Spring Authorization Server 打造认证中心(二)自定义数据库表
spring boot·后端·kotlin
开源之眼22 分钟前
github star 较多的Java双亲委派机制【类加载的核心内容加星】
java
编程火箭车24 分钟前
【Java SE 基础学习打卡】19 运算符(中)
java·java入门·运算符·编程基础·赋值运算符·复合赋值·自增自减
是一个Bug24 分钟前
Spring事件监听器源码深度解析
java·数据库·spring
蜂蜜黄油呀土豆29 分钟前
ThreadLocal 深度解析:它解决了什么、原理是什么、如何正确使用(含代码与实战建议)
java·并发编程·内存泄漏·threadlocal
45288655上山打老虎31 分钟前
【智能指针】
开发语言·c++·算法