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;
	}
}

  

 

转载于:https://www.cnblogs.com/simpledev/p/3554175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值