<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    潛魚在淵

    Concentrating on Architectures.

    posts - 77, comments - 309, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    JavaMail: a case of AuthenticationException.

    Posted on 2008-09-19 01:34 非魚 閱讀(1327) 評論(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.
    主站蜘蛛池模板: 亚洲视频在线观看免费视频| 亚洲一级片内射网站在线观看| 香蕉视频在线观看亚洲| h片在线播放免费高清| 亚洲第一网站男人都懂| 日本特黄特色AAA大片免费| 免费成人午夜视频| 一级美国片免费看| 亚洲av无码乱码国产精品| 免费一级毛片无毒不卡| 亚洲黄色免费网址| 日本h在线精品免费观看| 亚洲综合伊人制服丝袜美腿| 999在线视频精品免费播放观看| 亚洲福利电影在线观看| 久久精品无码一区二区三区免费| 亚洲jjzzjjzz在线观看| 国产片免费福利片永久| 国产精品hd免费观看| 久久亚洲精品中文字幕无码| 99re免费99re在线视频手机版| 亚洲喷奶水中文字幕电影| 色视频色露露永久免费观看| 国产尤物在线视精品在亚洲| 亚洲人成网77777亚洲色| 97国产免费全部免费观看| 色偷偷尼玛图亚洲综合| 国产亚洲AV手机在线观看| **真实毛片免费观看 | 亚洲最新永久在线观看| 91在线视频免费播放| 国产精品久久久久久亚洲小说| 国产亚洲色婷婷久久99精品91| 色欲国产麻豆一精品一AV一免费| 亚洲一区二区三区四区视频| 免费一级一片一毛片| 57pao国产成视频免费播放| WWW国产亚洲精品久久麻豆| 亚洲AV无码乱码在线观看富二代| 大地资源二在线观看免费高清| xxxxx做受大片在线观看免费|