我正在創建一個應用程序用於在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!");
}
}
}
您能否添加異常的堆棧跟蹤?你假設你的例外是由一個認證問題給出的,但可能不是。我還可以建議您嘗試使用端口578,而不是25,因爲最後一個可能會被網絡(防火牆)或您的ISP阻止。 – dic19
端口像578或25工作,但不是在第一次登錄! –
聽起來很奇怪。請添加例外的堆棧跟蹤,以便更多人能夠幫助您。 – dic19