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 分钟前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
平凡的小码农15 分钟前
JAVA实现大写金额转小写金额
java·开发语言
一直在进步的派大星18 分钟前
Docker 从安装到实战
java·运维·docker·微服务·容器
老华带你飞21 分钟前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
yttandb29 分钟前
重生到现代之从零开始的C语言生活》—— 内存的存储
c语言·开发语言·生活
我明天再来学Web渗透32 分钟前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
结衣结衣.1 小时前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
程序员陆通1 小时前
Spring Boot RESTful API开发教程
spring boot·后端·restful
茫茫人海一粒沙1 小时前
Python 代码编写规范
开发语言·python
原野心存1 小时前
java基础进阶知识点汇总(1)
java·开发语言