Java:javax.mail通过163服务器发送邮件

本文介绍了在SpringBoot项目中使用Java发送电子邮件遇到的问题,如SSL和端口设置,以及如何通过添加`mail.smtp.ssl.enable`和修改SMTP端口解决连接163.com邮箱的SSL相关错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
package com.example.demo;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendEmail {

    public static Properties getProperties(){

        Properties properties = new Properties();

        // 开启debug调试
        properties.setProperty("mail.debug", "true");
        // 邮件服务器
        properties.setProperty("mail.smtp.host", "smtp.163.com");
        // 端口号
        properties.setProperty("mail.smtp.port", "25");
        // 需要身份验证
        properties.setProperty("mail.smtp.auth", "true");

        // 发送邮件协议
        properties.setProperty("mail.transport.protocol", "smtp");
        
        properties.setProperty("mail.smtp.ssl.enable", "true");
        
        return properties;

    }

    public static void main(String[] args) {

        // 发件人
        String fromUser = "xxx@163.com";
        // 客户端授权码
        String password = "xxx";
        // 收件人
        String toUser = "xxx@qq.com";

        // 获取默认session对象
        Session session = Session.getInstance(getProperties());

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // 发送人
            message.setFrom(new InternetAddress(fromUser));

            // 接收人
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toUser));

            // 标题
            message.setSubject("This is the Subject Line!");

            // 消息体
            //message.setText("This is actual message");

            //设置消息体StringhtmlContent="<h1>Hello World!</h1><p>This is a HTML email.</p>";
            message.setContent("htmlContent", "text/html;charset=utf-8");

            // 发送消息
            Transport transport = session.getTransport();
            transport.connect(fromUser, password);
            transport.sendMessage(message, new Address[]{new InternetAddress(toUser)});
            transport.close();

            System.out.println("Sent message successfully....");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

SpringBoot项目部署到服务器上无法发送邮件解决方式

错误一:

trying to connect to host “smtp.163.com”, port 25, isSSL false

在发送邮件的类加上: props.put("mail.smtp.ssl.enable", true);

错误二:

trying to connect to host “smtp.163.com”, port 25, isSSL true

将SMTP端口号25改为465:
props.put("mail.smtp.port", "465");

怎么发送邮箱验证码javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2211) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740) at javax.mail.Service.connect(Service.java:388) at javax.mail.Service.connect(Service.java:246) at javax.mail.Service.connect(Service.java:195) at javax.mail.Transport.send0(Transport.java:254) at javax.mail.Transport.send(Transport.java:124) at com.lxy.sys.service.email.QQEmailSender.sendVerificationCode(QQEmailSender.java:57) at com.lxy.sys.service.email.QQEmailSender.main(QQEmailSender.java:78) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) at sun.security.ssl.Handshaker.process_record(Handshaker.java:914) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:400) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175) ... 8 more Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387) at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) at sun.security.validator.Validator.validate(Validator.java:260) at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460) ... 19 more Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145) at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131) at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382) ... 25 more 发送失败,请检查配置
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值