我遇到了一个非常令人沮丧的代码问题,该代码试图通过gmail通过JavaMail发送电子邮件。我的代码在Eclipse中运行时可以工作,但当我运行Ant时,它无法连接,并出现"Cannot connect to SMTP host,repense-1"错误。
我在Eclipse和Ant中使用相同的JRE,从类路径的角度看没有任何不同。
JavaMail调试日志如下——首先是在Eclipse中成功运行;
DEBUG: JavaMail version 1.4.2
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
Email Recipients = <email removed>
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 "smtp.gmail.com", port 465, isSSL false
220 smtp.gmail.com ESMTP b14sm9639869itd.15 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
现在是Ant的失败运行;
[java] Emailing skip report...
[java] DEBUG: JavaMail version 1.4.2
[java] DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
[java] DEBUG: Tables of loaded providers
[java] 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]}
[java] 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]}
[java] DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
[java] Email Recipients = <email address hidden>
[java] DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
[java] DEBUG SMTP: useEhlo true, useAuth true
[java] DEBUG SMTP: useEhlo true, useAuth true
[java] DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
[java] javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
[java] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1533)
[java] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
[java] at javax.mail.Service.connect(Service.java:313)
[java] at javax.mail.Service.connect(Service.java:172)
[java] at javax.mail.Service.connect(Service.java:121)
[java] at javax.mail.Transport.send0(Transport.java:190)
[java] at javax.mail.Transport.send(Transport.java:120)
[java] at com.diona.skipped.record.EmailReport.sendReportEmail(Unknown Source)
[java] at com.diona.skipped.record.SkipAlert.processLogs(Unknown DEBUG SMTP: EOF: [EOF]
[java] DEBUG SMTP: could not connect to host "smtp.gmail.com", port: 465, response: -1
有什么想法吗?
Chris
您是否在与从Eclipse运行时相同的机器上从ant运行?JavaMail常见问题解答提供了连接调试提示。此外,您使用的是非常旧的JavaMail版本;如果可以的话,升级到当前的1.5.5版本。