You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Java Mail API发邮件遇SMTPSendFailedException与MailConnectException

使用Java Mail API发送邮件时遇到的异常问题

第一个异常:SMTPSendFailedException

尝试发送邮件时持续触发SMTPSendFailedException,完整错误信息如下:

org.eclipse.angus.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. d5-20020a170902c18500b00194c90ca320sm6172077pld.204 - gsmtp

    at org.eclipse.angus.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2422)
    at org.eclipse.angus.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1839)
    at org.eclipse.angus.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1316)

发送邮件的代码实现

以下是sendEmail()函数的代码:

public void sendEmail(List<String> to, String subject, String body){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.starttls.enable", true);

        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        try {
            // 创建邮件消息
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(username));
            InternetAddress[] addresses = new InternetAddress[to.size()];
            for (int i = 0; i < to.size(); i++) {
                addresses[i] = new InternetAddress(to.get(i));
            }
            msg.setRecipients(Message.RecipientType.TO, addresses);
            msg.setSubject(subject);
            msg.setText(body, "utf-8");
            msg.setSentDate(new Date());
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", username, appPassword);// 触发异常的位置
            transport.sendMessage(msg, msg.getAllRecipients());
//            transport.send(msg);
        } catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
        }
    }

其中username和appPassword为全局变量。

替换发送方式后的异常:MailConnectException

当改用transport.send(msg)替代transport.sendMessage(msg, msg.getAllRecipients())时,出现MailConnectException,错误信息如下:

org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at org.eclipse.angus.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2258)
    at org.eclipse.angus.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:753)
    at jakarta.mail.Service.connect(Service.java:342)
    at jakarta.mail.Service.connect(Service.java:222)
    at jakarta.mail.Service.connect(Service.java:171)
    at jakarta.mail.Transport.send0(Transport.java:230)
    at jakarta.mail.Transport.send(Transport.java:100)

尽管已配置端口587,仍出现该错误。

使用的Java Mail API依赖

<dependency>
            <groupId>org.eclipse.angus</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>1.0.0</version>
</dependency>

已尝试Stack Overflow及ChatGPT的多种方案,但仍未解决问题。


内容的提问来源于stack exchange,提问作者Mansi Keshari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:31:02