javax.mail由于HELO / EHLO参数“localhost”无效,发送电子邮件失败

时间:2018-02-20 17:10:36

标签: server localhost javamail

我正在使用javax.mail从我的后端发送电子邮件到我的个人电子邮件地址(如果发生特殊情况,我想收到通知)。 在我的本地机器上(即处于调试/开发模式),这很好用。但是在我的生产服务器上,我遇到了以下问题:

DEBUG: JavaMail version 1.5.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,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP q2sm11403478qtf.52 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO localhost,
501-5.5.4 HELO/EHLO argument "localhost," invalid, closing connection.
501 5.5.4  https://support.google.com/mail/?p=helo q2sm11403478qtf.52 - gsmtp
HELO localhost,
DEBUG SMTP: EOF: [EOF]
javax.mail.MessagingException: [EOF]
    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2215)
    at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:1599)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:705)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)

显然HELO/EHLO argument "localhost," invalid是问题所在。我首先想到我必须更改/etc/hosts个条目。但实际上他们应该没问题,他们是这样的:

$ cat /etc/hosts
127.0.0.1 localhost, myremotemachine
87.85.108.234   myremotemachine

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

因此,我将服务器名称映射到127.0.0.1和我的公共IP地址。此外,在控制台中,命令hostname输出myremotemachine

那么,我还需要做些什么?

1 个答案:

答案 0 :(得分:1)

首先,/ etc / hosts文件中的名称之间不应有逗号。

您可以将87.85.108.234条目移到localhost条目之上,或从localhost条目中删除myremotemachine,或将JavaMail mail.smtp.localhost属性设置为您要使用的名称。