<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.
    主站蜘蛛池模板: 成人亚洲网站www在线观看| 亚洲Av无码国产情品久久| 色窝窝免费一区二区三区| 亚洲成a人片77777老司机| 亚洲免费在线观看视频| 久久国产高潮流白浆免费观看| 亚洲va在线va天堂va不卡下载 | 免费视频中文字幕| 亚洲午夜在线播放| 成在人线AV无码免费| 亚洲女女女同性video| 国产国产成年年人免费看片| 亚洲av无码一区二区三区乱子伦| 亚洲av无码专区亚洲av不卡| 免费看韩国黄a片在线观看| 亚洲中文字幕无码久久2020| 国产高清在线精品免费软件| www成人免费视频| 最新国产AV无码专区亚洲| 久草福利资源网站免费| 亚洲日本一区二区三区在线不卡| 日韩毛片在线免费观看| 天天影院成人免费观看| 国产精品亚洲mnbav网站| a级毛片免费高清毛片视频| 亚洲午夜在线电影| a级毛片视频免费观看| 久久久久亚洲Av无码专| 一本无码人妻在中文字幕免费| 亚洲国产精品无码久久| 久久WWW免费人成人片| 免费手机在线看片| 亚洲AV无码不卡在线播放| 成人片黄网站A毛片免费| 男女啪啪免费体验区| 亚洲国产高清在线| 日本高清免费不卡视频| www免费黄色网| 亚洲国产精品综合福利专区| 国产免费爽爽视频免费可以看| 亚洲国产精品嫩草影院|