javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端異常解決


在項目里面使用javamail在window環境正常,放在服務器上面的時候拋出異常javax.mail.MessagingException: 501 Syntax: HELO hostname ,原因是在linux無法解析郵件服務器名稱為ip地址,解決方法有二種:

第一種,在linux服務器上面,/etc/hosts

127.0.0.1       localhost  
::1             localhost6.localdomain6 localhost6 

 

第二種,在java代碼里面配置 props.put("mail.smtp.localhost", "127.0.0.1");這事關鍵的地方~!

Mail.java

/**
 * @author huangjing
 * @date 2014-2-13
 */
public class Mail {
	static int _PORT = 465; // smtp端口
//	static String _SERVER = "smtp.exmail.qq.com"; // smtp服務器地址
	static String _SERVER = "113.108.16.119";
//	static String _FROM = "huangjing@yangchehome.com"; // 發送者
	static String _FROM = "養車之家"; // 發送者
	static String _USER = "customer_service@yangchehome.com"; // 發送者地址
	static String _PASSWORD = "郵箱的密碼"; // 密碼
	
	static String _PC_IP = "127.0.0.1";
}

  

SendMail.java

public class SendMail {
	private static String ERROR_MASSAGE = "郵件發送失敗,請稍后再試!";
	private static String SUCCESS_MASSAGE = "郵件發送成功!";
	
	private Logger logger = Logger.getLogger(SendMail.class);

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public boolean sendMain(String subject, String content, String to)
			throws UnsupportedEncodingException {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", Mail._SERVER);
			props.put("mail.smtp.port", Mail._PORT);
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.localhost", Mail._PC_IP);
			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(Mail._SERVER, Mail._USER, Mail._PASSWORD);
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
//			InternetAddress fromAddress = new InternetAddress(
//					Mail._USER, MimeUtility.encodeText(new String(
//							Mail._FROM.getBytes("ISO-8859-1"),
//							"UTF-8"), "gb2312", "B"));
			InternetAddress fromAddress = new InternetAddress(
					Mail._USER, MimeUtility.encodeText(Mail._FROM, "gb2312", "B"));
			//編碼方式有兩種:"B"代表Base64、"Q"代表QP(quoted-printable)方式。
			
			msg.setFrom(fromAddress);
			InternetAddress toAddress = new InternetAddress(to);
			msg.setRecipient(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");
			msg.setText(content, "UTF-8");
			msg.saveChanges();

			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return false;
		}
		return true;
	}

	public boolean sendMainHTML(String subject, String content, String to)
			throws UnsupportedEncodingException {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", Mail._SERVER);
			props.put("mail.smtp.port", Mail._PORT);
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.localhost", Mail._PC_IP);

			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(Mail._SERVER, Mail._USER, Mail._PASSWORD);
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
//			InternetAddress fromAddress = new InternetAddress(
//					Mail._USER, MimeUtility.encodeText(
//							new String(Mail._FROM
//									.getBytes("ISO-8859-1"), "UTF-8"),
//							"gb2312", "B"));
			InternetAddress fromAddress = new InternetAddress(
					Mail._USER, MimeUtility.encodeText(
							Mail._FROM, "gb2312", "B"));
			//編碼方式有兩種:"B"代表Base64、"Q"代表QP(quoted-printable)方式。
			
			msg.setFrom(fromAddress);
			InternetAddress toAddress = new InternetAddress(to);
			msg.setRecipient(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");
			msg.setContent(content, "text/html;charset=UTF-8");
			msg.saveChanges();

			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			return false;
		}
		return true;
	}
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM