501 Command "HELO" requires a argument problem solving method _java

Source: Internet
Author: User

Scenario Description:
When you save a mailbox configuration, the mailbox configuration parameters are automatically detected and the results show that when you probe SMTP, the system reports the following exceptions:

Copy Code code as follows:

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)

But to change a Windows server, found that there is no such problem, only on a Linux server can reproduce, intuitive feeling is that this Linux server some configuration problems.

Troubleshooting steps
1. Find a Linux server with the same operating system, verify the mailbox configuration, OK, and troubleshoot the Linux operating system specific problems
2. Use Telnet directly on the Linux server to connect the SMTP port to the End-to-end mail server, OK, and troubleshoot the network problem on the server
3. Find Helo Instruction explanation
Copy Code code as follows:

HELO
--Initiates a conversation with the mail server. When using this command, you can specify your domain name and the mail server knows who you are. For example, HELO mailhost2. cf.ac.uk.

Discover that the HELO directive needs to be followed by the host name of an initiator, telling the SMTP server where the source of the message is.
Looking at the exception information is "501 Command" HELO "requires an argument", it is obvious that the program in the interaction with the SMTP server does not pass the source host domain name.

4. View Java Mail Source code
Look for helo directives as follows:

Copy Code code as follows:

/**
* 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);
}

Find out where the Helo method is invoked to see how domain is passed over
Copy Code code as follows:

if (Useehlo)
succeed = EHLO (Getlocalhost ());
if (!succeed)
Helo (Getlocalhost ());

Logically, then find the Getlocalhost () method, defined as follows:
Copy Code code as follows:

/**
* Get the name of the ' local host ' for 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 we 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 my name, use the local address literal
if (localhostname = null)
Xxx-not correct for IPv6
Localhostname = "[" + localhost.gethostaddress () + "]";
}
catch (Unknownhostexception Uhex) {
}
return localhostname;
}

You can see that the hostname can be obtained from the session properties of the current connection, or from the hosts configuration of the current server, and our program is not set hostname in session, The reason for this is that the hosts file for the Linux server has been modified, causing the Java program to not automatically get localhostname.

5. View the/etc/hosts file as follows:

Copy Code code as follows:

127.0.0.1 localhost.localdomain localhost
:: 1 localhost6.localdomain6 Localhost6

Simply modify the Hosts file as follows:
Copy Code code as follows:

127.0.0.1 localhost
:: 1 localhost6.localdomain6 Localhost6

6. Re-test, the problem is solved.
In fact, this situation can also be avoided by the program, that is, in the session join the current server's Hostname property, program example:
Copy Code code as follows:

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 server connection failed, please check the input information is correct");
catch (Nosuchproviderexception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("SMTP server connection failed, please check the input information is correct");
catch (Messagingexception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("SMTP server connection failed, please check the input information is correct");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.