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

时间:2013-07-16 05:49:57

标签: java java-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");
    }
});

1 个答案:

答案 0 :(得分:1)

来自Java Mail API FAQ

  

您应该致电trans.sendMessage(msg, addrs)发送消息。如上所述,send方法是一种静态便捷方法,它获取自己的Transport对象并创建自己的连接以用于发送;它不使用与调用它的任何Transport对象关联的连接。

简而言之,请勿致电Transport.send(...),而是致电Transport.sendMessage(...)