501 Command "HELO" requires an argument成績的處理辦法。本站提示廣大學習愛好者:(501 Command "HELO" requires an argument成績的處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是501 Command "HELO" requires an argument成績的處理辦法正文
場景描寫:
保留郵箱設置裝備擺設時主動探測郵箱設置裝備擺設參數能否准確,成果發明在探測SMTP時,體系報出以下異常:
javax.mail.MessagingException: 501 Command "HELO" requires an argument
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
at javax.mail.Service.connect(Service.java:275)
然則換一個windows辦事器,發明就沒如許的成績,僅在一台Linux辦事器上可以重現,直不雅感到就是這台Linux辦事器某些設置裝備擺設有成績。
排查步調
1. 找一台異樣操作體系的Linux辦事器,驗證郵箱設置裝備擺設,ok,消除Linux操作體系特別性的成績
2. 直接在Linux辦事器上應用telnet銜接對端郵件辦事器的SMTP端口,OK,消除該辦事器的收集成績
3. 查找HELO指令說明
HELO
-- Initiates a conversation with the mail server. When using this command you can specify your domain name so that the mail server knows who you are. For example, HELO mailhost2. cf.ac.uk.
發明HELO指令前面須要跟一個提議者的主機名,告知SMTP辦事器這個新聞起源是哪裡。
再看異常信息是"501 Command "HELO" requires an argument",很顯著,法式在跟SMTP SERVER交互進程中沒有傳遞源主機域名。
4. 檢查JAVA MAIL源碼
查找HELO指令,以下:
/**
* Issue the <code>HELO</code> command.
*
* @param domain
* our domain
*
* @since JavaMail 1.4.1
*/
protected void helo(String domain) throws MessagingException {
if (domain != null)
issueCommand("HELO " + domain, 250);
else
issueCommand("HELO", 250);
}
查找helo辦法在哪裡被挪用,看看domain若何被傳遞過去的
if (useEhlo)
succeed = ehlo(getLocalHost());
if (!succeed)
helo(getLocalHost());
瓜熟蒂落,接著找getLocalHost()辦法,界說以下:
/**
* Get the name of the local host, for use in the EHLO and HELO commands.
* The property mail.smtp.localhost overrides mail.smtp.localaddress, which
* overrides what InetAddress would tell us.
*/
public synchronized String getLocalHost() {
try {
// get our hostname and cache it for future use
if (localHostName == null || localHostName.length() <= 0)
localHostName = session.getProperty("mail." + name + ".localhost");
if (localHostName == null || localHostName.length() <= 0)
localHostName = session.getProperty("mail." + name+ ".localaddress");
if (localHostName == null || localHostName.length() <= 0) {
InetAddress localHost = InetAddress.getLocalHost();
localHostName = localHost.getHostName();
// if we can't get our name, use local address literal
if (localHostName == null)
// XXX - not correct for IPv6
localHostName = "[" + localHost.getHostAddress() + "]";
}
} catch (UnknownHostException uhex) {
}
return localHostName;
}
可以看到hostname的獲得可以經由過程以後銜接的session屬性中獲得,也能夠從以後辦事器的hosts設置裝備擺設中獲得,而我們法式是沒有在session中設置hostname的,是以緣由確定在於該台Linux辦事器的hosts文件被修正,形成JAVA法式沒法主動取得localhostName。
5. 檢查/etc/hosts文件,情形以下:
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
簡略的將hosts文件修正以下:
127.0.0.1 localhost
::1 localhost6.localdomain6 localhost6
6. 從新測試,成績處理了。
其實,這類情形也能夠經由過程法式防止,即在session銜接中參加以後辦事器的hostname屬性,法式示例:
public static void main(String[] args) {
try {
int smtpport = 25;
String smtpserver = "219.147.xxx.xxx";
Properties prop = System.getProperties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.localhost", "myMailServer");
Session mailSession = Session.getInstance(prop, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect(smtpserver,smtpport, "username", "password");
System.out.println("connect ok");
transport.close();
} catch (AuthenticationFailedException en) {
en.printStackTrace();
System.out.println("smtp辦事器銜接掉敗,請檢討輸出信息能否准確");
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.out.println("smtp辦事器銜接掉敗,請檢討輸出信息能否准確");
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("smtp辦事器銜接掉敗,請檢討輸出信息能否准確");
}
}