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;
}
}
javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决的更多相关文章
- 【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邮箱 ...
- spring -java.lang.NoClassDefFoundError: javax/mail/MessagingException
今天遇到这个问题,网上找了半天,终于解决了,最后记录一下. spring集成了mail,于是就测试了下,结果报了java.lang.NoClassDefFoundError: javax/mail/M ...
- 使用 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 ...
- javax.mail 遇到501 mail from address must be same as authorization user 的問題
使用不同的兩個帳戶发送email时,第一个账户可以发送成功,但到第二个账户的时候就报出了501 mail from address must be same as authorization user ...
- 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 ...
- javax.mail 发送邮件异常
一.运行过程抛出异常 1.Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/ ...
- java使用javax.mail进行免费的邮件发送
1. 建议发送方使用阿里云邮箱https://mail.aliyun.com/,阿里云默认是开启个人邮箱pop3.smtp协议的,所以无需在阿里云邮箱里设置,pop3.smtp的密码默认邮箱登录密码, ...
- 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)
Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...
- javax.mail用smtp服务器发送带附件的邮件
jar包: javax.mail-1.5.5.jar maven配置: <dependency> <groupId>com.sun.mail</groupId> & ...
随机推荐
- 8个超炫酷的纯CSS3动画及源码分享
在现代网页中,我们已经越来越习惯使用大量的CSS3元素,而现在的浏览器也基本都支持CSS3,所以很多时候我们不妨思考一下是否可以用纯CSS3制作一些有趣或者实用的网页.本文要分享8个超炫酷的纯CSS3 ...
- GDAL中RasterIO函数(把文件读取为一个一维数组)和ReadBlock函数(读取栅格数据块)
CPLErr GDALRasterBand::RasterIO ( GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, ...
- daily news新闻阅读客户端应用源码(兼容iPhone和iPad)
daily news新闻阅读客户端应用源码(兼容iPhone和iPad),也是一款兼容性较好的应用,可以支iphone和ipad的阅读阅读器源码,设计风格和排列效果很不错,现在做新闻资讯客户端的朋友可 ...
- 8.samba server与client配置
server端 1.安装samba:yum install -y samba\* 增加samba用户: useradd smb用户名 smbpasswd -a smb用户名 ...
- Android发送请求到不同的Servlet,但都是一个Servlet处理
错误原因,在Servlet文件中 @WebServlet("/ServletForGETMethod") 与实际的ServletForQUERYMethod 文件名不符. @Web ...
- 关于angularJS与jquery在使用上的一些感悟
最近做的项目中,有同时用到angularJS与jquery两种JS框架. 在使用过程中发现,angularJS的用法更像是面向对象的编程模式.它会要求你定义一个view model,然后所有的页面变化 ...
- Sandcastle是什么
如果你的项目是.net开发,同时需要生成HTML的方法成员文档时,哪么就不得不拿出Sandcastle 因为Sandcastle是微软开发,并开源的文档生成工具; 这种生成进度等待的感觉很爽! 在这里 ...
- Python学习第五天
复习内容: · 迭代器&生成器 · 装饰器 · Json & pickle 数据序列化 · 软件目录结构规范yi 一.生成器 1. 列表生成式: 2. 生成器的定义:在Pyth ...
- eclipse 安装插件不生效
操作: 下载了一个插件,插件中只有一个jar包,解压放到plugins目录,插件没生效. 解决方法: 把jar包直接扔到dropins目录下,并且删除eclipse\configuration\org ...
- 使用Azure portal Create Virtual Machine
使用简单快速的方式穿件的Virtual Machine 这个步骤隐藏的了很多步骤,例如的创建的云服务(Cloud Service) 创建存储(Storage) 存储名为系统自动产生 可以通过存储看到含 ...