来源:http://blog.csdn.net/hbcui1984/article/details/5655657?reload
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.
/** * 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); }
if (useEhlo) succeed = helo(getLocalHost()); if (!succeed) helo(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; }
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", "mylocalserverdomain"); 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服务器连接失败,请检查输入信息是否正确"); } }