潜鱼在渊

Concentrating on Architectures.

posts - 77, comments - 309, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JavaMail: a case of AuthenticationException.

Posted on 2008-09-19 01:34 非鱼 阅读(1323) 评论(2)  编辑  收藏 所属分类: Java技术
Several days ago I found that in a certain environment JavaMail may throw an AuthenticationException even you have set right user/password. Maybe this will not happen in a different mail server, but I'm not sure. So I just write it down and hope this can help someone who encounters such a problem.

Firstly I'd like to say the code is complied to specifications. The snippet:

props = new Properties();
props.put( 
"mail.smtp.host", mailhost );
props.put( 
"mail.smtp.port", port );
props.put(
"mail.user", username);
props.put(
"mail.password", password);
props.put(
"mail.smtp.auth""true");

javax.mail.Session session 
= javax.mail.Session.getInstance(props);

Message msg 
= new MimeMessage(session);
msg.setFrom( new InternetAddress(  from  ) ); // from is "user@localhost".

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse( recipient, false ) );
msg.setSubject( subject );
msg.setContent(text, 
"text/html");
msg.setSentDate(
new Date());        

// send the thing off
Transport transport  = null;
try {
  PasswordAuthentication pa 
= new PasswordAuthentication((String)props.get("mail.user"),
        (String)props.get(
"mail.password"));    
  transport 
= session.getTransport("smtp");    
  URLName urlname 
= transport.getURLName();
  session.setPasswordAuthentication(urlname, pa);
 
if (!transport.isConnected()) {    
    transport.connect();        
  }
  Transport.send(msg);
catch (MessagingException e) {    
 
// TODO Auto-generated catch block    
  e.printStackTrace();    
 
throw e;
finally {    
  transport.close();
}

The code was tested ok in another program, and, use the same email server (Coremail). But whenever I debug this one, I got an AuthenticationException.

Nearly everything was the same to the another program, but of cause, actually not.

At last I opened the debug mode, like this:

javax.mail.Session session = javax.mail.Session.getInstance(props);
session.setDebug(
true);
session.setDebugOut(System.out);


Then I found it out.

msg.setFrom( new InternetAddress(  from  ) ); // from is "user@localhost".


The mail server not only need a user account and password, but also the right msg.setFrom().

After I set "from" to my real email address regarding to the mail server, everything went right.

And the conclusion is, as I guess, that some mail server would only accept an account in its system to send a mail. I dont know if this rule is suitable to all mail servers, because I have no chance to try others.


By Feelyou, under CC License. Technorati 标签: ,

评论

# re: JavaMail: a case of AuthenticationException.  回复  更多评论   

2008-09-19 09:35 by 隔叶黄莺
是你的原创吗?那要赞一个

# re: JavaMail: a case of AuthenticationException.  回复  更多评论   

2008-09-19 12:01 by 非鱼
@隔叶黄莺
Thank you for your appreciations.

只有注册用户登录后才能发表评论。


网站导航: