2013-08-22 63 views
0

我正在創建一個應用程序用於在java中發送電子郵件,所有設置都正常並且正常工作。但錯誤是當我嘗試登錄Hotmail帳戶時,它顯示錯誤的電子郵件和密碼,我在catch塊中給出了錯誤。 但是,當我第一次登錄yahoo或gmail時,它登錄到hotmail,之後我可以在Hotmail登錄,但是我不能先登錄hotmail,爲什麼?代碼如下
java中的hotmail登錄錯誤(IDE:Netbeans)

private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {           
    user = txt_user.getText(); 
    pass = txt_pass.getText(); 
    int combo = combo_ac.getSelectedIndex(); 

    if(combo==0){ 
try{ 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

     Session sessin = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication(){ 
        return new PasswordAuthentication(user+"@gmail.com", pass); 
       } 
      } 
     ); 
    new Gmail().setVisible(true); 
}catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "Incorrect Email or Password!"); 
} 
    }else if(combo==1){ 
try{ 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.live.com"); 
     props.put("mail.smtp.socketFactory.port", "25"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "25"); 

     Session sessin = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication(){ 
        return new PasswordAuthentication(user+"@hotmail.com", pass); 
       } 
      } 
     ); 
    new Hotmail().setVisible(true); 
}catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "Incorrect Email or Password!"); 
} 
    }else if(combo==2){ 
try{ 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.mail.yahoo.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

     Session sessin = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication(){ 
        return new PasswordAuthentication(user+"@yahoo.com", pass); 
       } 
      } 
     ); 
    new Yahoo().setVisible(true); 
}catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "Incorrect Email or Password!"); 
} 
    } 
}           
+1

您能否添加異常的堆棧跟蹤?你假設你的例外是由一個認證問題給出的,但可能不是。我還可以建議您嘗試使用端口578,而不是25,因爲最後一個可能會被網絡(防火牆)或您的ISP阻止。 – dic19

+0

端口像578或25工作,但不是在第一次登錄! –

+0

聽起來很奇怪。請添加例外的堆棧跟蹤,以便更多人能夠幫助您。 – dic19

回答

1

article可以解釋爲什麼顯然您可以登錄到您的Hotmail帳戶登錄到Gmail或雅虎之後。實際上,你正在登錄到剛剛嘗試的同一帳戶。事情是你使用Session#getDefaultInstance()代替Session#getInstance()

Session.getDefaultInstance方法創建一個新的Session 第一 時間這就是所謂的,使用所傳遞的屬性。 後續 調用將返回原始會話並忽略您通過 傳入的任何屬性。如果要使用不同的 屬性創建不同的會話,則Session.getDefaultInstance不會這樣做。 [...]始終使用Session.getInstance來避免此問題。

但它並不能解釋爲什麼你不能登錄到你的hotmail帳戶,但你應該發佈異常堆棧跟蹤,以便我們可以幫助你。

更新:

在此基礎上堆棧跟蹤:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first 

這意味着你必須在true設置mail.smtp.starttls.enable財產按Hotmail的要求:

選擇TLS在下使用以下類型的加密連接傳出服務器(SMTP),然後單擊確定。注意發送服務器如果端口25被阻止在 網絡或您的ISP (SMTP)框中應設置爲端口25,你可以設置SMTP端口爲587

注:摘自Configure Outlook to connect to an MSN email account

您應該只嘗試登錄到您的Hotmail帳戶時,將下面的行添加到您的代碼:

props.put("mail.smtp.starttls.enable", "true"); 

也不要忘記Session.getInstance更換Session.getDefaultInstance正如我上面建議。

+0

異常堆棧跟蹤是:'com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必須首先發出STARTTLS命令' –

+0

@RafiAbro回答更新,請讓我知道它是否工作。 – dic19

+1

感謝哥們!它的工作非常好! :) –