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

  

javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决的更多相关文章

  1. 【Linux】【Jenkins】邮件发送失败的问题javax.mail.MessagingException: Could not connect to SMTP host:

    javax.mail.MessagingException: Could not connect to SMTP host: smtp.126.com,port:25 解决方案: 之前用的是126邮箱 ...

  2. spring -java.lang.NoClassDefFoundError: javax/mail/MessagingException

    今天遇到这个问题,网上找了半天,终于解决了,最后记录一下. spring集成了mail,于是就测试了下,结果报了java.lang.NoClassDefFoundError: javax/mail/M ...

  3. 使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response

    使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.Messagin ...

  4. javax.mail 遇到501 mail from address must be same as authorization user 的問題

    使用不同的兩個帳戶发送email时,第一个账户可以发送成功,但到第二个账户的时候就报出了501 mail from address must be same as authorization user ...

  5. javax.mail.MessagingException: Could not connect to SMTP host: smtp.xdf.cn

    1.问题描述:关于使用Java Mail进行邮件发送,抛出Could not connect to SMTP host: xx@xxx.com, port: 25的异常可能: 当我们使用Java Ma ...

  6. javax.mail 发送邮件异常

    一.运行过程抛出异常 1.Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/ ...

  7. java使用javax.mail进行免费的邮件发送

    1. 建议发送方使用阿里云邮箱https://mail.aliyun.com/,阿里云默认是开启个人邮箱pop3.smtp协议的,所以无需在阿里云邮箱里设置,pop3.smtp的密码默认邮箱登录密码, ...

  8. 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)

    Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...

  9. javax.mail用smtp服务器发送带附件的邮件

    jar包: javax.mail-1.5.5.jar maven配置: <dependency> <groupId>com.sun.mail</groupId> & ...

随机推荐

  1. javascript之基本包装类型(Boolean,Number,String)基础篇

    前几天整理了javascript中Array方面的知识,但是String中的一些方法多多少少和Array里的方法有些类似容易混淆,就顺便连同String所在的包装类一起整理一下,希望可以帮助到初学者, ...

  2. 关于css的一些小细节---link

    <link rel="stylesheet" href=“a.css” type="text/css"> rel:当前文档与被链接文档间的关系,必须 ...

  3. Eclipse的常用快捷方式

    在开发中,常用的一些快捷方式 alt+shift+r 修改方法名,变量名,关键体会enter                 ctrl+shift+c 先选中n行,注释                 ...

  4. 判断Featureclass的类型

    一个Featureclass可以是Shapefile Feature Class.Personal Geodatabase Feature Class.File Geodatabase Feature ...

  5. centos安装环境准备工作

    我们的centos系统安装好了,并且网络已经连通了,接下来介绍一下,在外网连通的情况下,我们如何安装tar.gz等形式的软件. centos安装后如果想作为正常应用development tools和 ...

  6. 安装配置 redis

    1.  cd /usr/ley/softwares 2.  wget  http://download.redis.io/redis-stable.tar.gz 3.  tar –xzf redis- ...

  7. NOSQL之【redis的安全策略】

    原文:http://redis.io/topics/security 1.Redis的安全模式 可信环境下的可信用户才可访问redis.这意味着,将redis服务器直接暴露在Internet或者不可信 ...

  8. CF 148A Insomnia cure

    题目链接:传送门 题目大意:就是给四个数,和一个d,问1-d中有多少个数字不是那四个数的倍数; 这道题的d数据很小直接暴力可以过: 暴力代码:时间复杂度O(1): #include<stdio. ...

  9. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

  10. Posix 共享内存区

    要点 与mmap配合使用 open与shm_open的区别,open打开磁盘上的普通文件,shm_open创建和打开的文件在/dev/shm文件夹下,该文件夹对应的是内存 gcc编译时加参数-lrt ...