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

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

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

127.0.0.1       localhost  
::1             localhost6.localdomain6 localhost6 

 

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

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.javasession

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"表明Base6四、"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"表明Base6四、"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;
	}
}
相關文章
相關標籤/搜索