javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决

时间:2022-12-03 18:09:08

在项目里面使用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;
	}
}