在项目里面使用javamail在window环境正常,放在服务器上面的时候抛出异常javax.mail.MessagingException: 501 Syntax: HELO hostname ,原因是在linux无法解析邮件服务器名称为ip地址,解决方法有二种:
第一种,在linux服务器上面,/etc/hosts
1
2 |
127.0.0.1 localhost ::1 localhost6.localdomain6 localhost6 |
第二种,在java代码里面配置 props.put("mail.smtp.localhost", "127.0.0.1");这事关键的地方~!
Mail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
/** * @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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 |
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端异常解决
原文:http://www.cnblogs.com/simpledev/p/3554175.html