java javax.mail.MessagingException 无法连接到 SMTP 主机端口:25 响应 -1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17668947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.mail.MessagingException could not connect to SMTP host port :25 response -1
提问by JaiK
I am getting a response code of -1 and I am not sure what is the workaround for this. The office has a firewall setting which cannot be disabled as well as antivirus running.Since the java
code is running from the office,how do I get this code working to email the testing report.
我收到了 -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)
Code to send the email:
发送电子邮件的代码:
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");
}
});
回答by mthmulders
From the Java Mail API FAQ:
You should call
trans.sendMessage(msg, addrs)
to send the message. As described above, thesend
method is a static convenience method that acquires its ownTransport
object and creates its own connection to use for sending; it does not use the connection associated with anyTransport
object through which it is invoked.
你应该打电话
trans.sendMessage(msg, addrs)
发送消息。如上所述,该send
方法是一个静态的便捷方法,它获取自己的Transport
对象并创建自己的连接用于发送;它不使用与Transport
调用它的任何对象关联的连接。
In short, don't call Transport.send(...)
, call Transport.sendMessage(...)
instead.
简而言之,不要打电话Transport.send(...)
,Transport.sendMessage(...)
而是打电话。