hotmail login error in java (IDE: Netbeans)

2019-08-02 20:05发布

I am creating an app for email sending in java, everything is set and working properly. but the error is when I try to login in Hotmail account it shows error of incorrect email and password which I have given in catch block. but It logins in hotmail when I first login in yahoo or gmail, after that I can login in Hotmail, but I can not login in hotmail first, Why?! Code is below

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条回答
手持菜刀,她持情操
2楼-- · 2019-08-02 20:30

This article would explain why apparently you can log in into your hotmail account after log in into gmail or yahoo. Actually you're logging in into the same account you just did at first try. The thing is you're using Session#getDefaultInstance() instead Session#getInstance():

The Session.getDefaultInstance method creates a new Session the first time it's called, using the Properties that are passed. Subsequent calls will return that original Session and ignore any Properties you pass in. If you want to create different Sessions with different properties, Session.getDefaultInstance won't do that. [...] Always use Session.getInstance to avoid this problem.

However it doesn't explain why you can't log in into your hotmail account but you should post the exception stack trace so we can help you.

Update:

Based on this stack trace:

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

This means you have to set mail.smtp.starttls.enable property on true as per Hotmail requirement:

Select TLS for Use the following type of encrypted connection under Outgoing server (SMTP), and then click OK. Note The Outgoing Server (SMTP) box should be set to port 25. If port 25 is blocked in the network or by your ISP, you can set SMTP port to 587.

Note: Extracted from Configure Outlook to connect to an MSN email account.

You should only add the following line to your code when trying to log in into your Hotmail account:

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

Also don't forget replace Session.getDefaultInstance by Session.getInstance as I suggested above.

查看更多
登录 后发表回答