javax.mail.MessagingException 无法连接到 SMTP 主机端口 :25 response -1

标签 java jakarta-ee testng

我收到的响应代码为 -1,但我不确定解决方法是什么。办公室有防火墙设置,无法禁用,防病毒软件也正在运行。由于 java 代码是从办公室运行的,我如何让该代码工作以通过电子邮件发送测试报告。

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@540408: 16 ms
DEBUG: JavaMail version 1.4.5
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxx.com", port 25, isSSL false
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: could not connect to host "xxxx.com", port: 25, response: -1

javax.mail.MessagingException: Could not connect to SMTP host: xxx.com, port: 25, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1960)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    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.seleniumtests.test.SendFileEmail.trySendEmail(SendFileEmail.java:131)
    at com.seleniumtests.test.Email.generateReport(Email.java:27)
    at org.testng.TestNG.generateReports(TestNG.java:1094)
    at org.testng.TestNG.run(TestNG.java:1053)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

发送电子邮件的代码:

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT );
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
        return new javax.mail.PasswordAuthentication("myusername","mypassword");
    }
});

最佳答案

来自Java Mail API FAQ :

You should call trans.sendMessage(msg, addrs) to send the message. As described above, the send method is a static convenience method that acquires its own Transport object and creates its own connection to use for sending; it does not use the connection associated with any Transport object through which it is invoked.

简而言之,不要调用 Transport.send(...),而是调用 Transport.sendMessage(...)

关于javax.mail.MessagingException 无法连接到 SMTP 主机端口 :25 response -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668947/

相关文章:

xml - 实体已被引用但未声明

java - 在 Eclipse 中运行 TestNG 单元测试会忽略最新更改

java - JAXB 为没有内容的标签设置一个值

java - 如何从 Jena 获取嵌套的 RDF/XML?

Node.js 中带有相对路径的 java 命令

java - 为什么在启动 REST 服务时添加注释 FormDataParam 会引发异常?

java - 如何在 Payara Micro 中添加 JDBC 驱动程序和配置 JDBC 资源?

java - Selenium 从范围报告 3.1.5 迁移到 4.1.6

java - TestNG - @BeforeMethod 用于特定方法

java - 在Java中显示char的ArrayList