java - java中的hotmail登录错误(IDE : Netbeans)

标签 java netbeans runtime-error email

我正在创建一个用java发送电子邮件的应用程序,一切都已设置并且工作正常。但错误是当我尝试登录 Hotmail 帐户时,它显示我在 catch block 中给出的电子邮件和密码不正确的错误。 但是当我第一次登录yahoo或gmail时,它会登录hotmail,之后我可以登录Hotmail,但我无法先登录hotmail,为什么?!代码如下

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!");
 }
    }
}                                         

最佳答案

这个article会解释为什么显然您可以在登录 gmail 或 yahoo 后登录您的 hotmail 帐户。实际上,您登录的帐户与您第一次尝试时使用的帐户相同。问题是您正在使用 Session#getDefaultInstance() 而不是 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.

但是,它并不能解释为什么您无法登录到您的 hotmail 帐户,但您应该发布异常堆栈跟踪,以便我们可以帮助您。

更新:

基于此堆栈跟踪:

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

这意味着您必须根据 Hotmail 要求将 mail.smtp.starttls.enable 属性设置为 true:

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.

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

尝试登录 Hotmail 帐户时,您应该仅将以下行添加到代码中:

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

另外,不要忘记按照我上面的建议将 Session.getDefaultInstance 替换为 Session.getInstance

关于java - java中的hotmail登录错误(IDE : Netbeans),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18388581/

相关文章:

java - ActiveMQ RedeliveryPolicy 未设置

java - 更改colors.xml中颜色的Android文本输入布局

java - ModelMapper 在将实体转换为 DTO 时产生异常

java - 使用 Java 创建按钮快捷方式,例如使用 Windows 窗体的 &(& 符号)

java - 线程 "main"java.lang.IllegalAccessError : failed to access class 中出现异常

java - Java 新手,似乎无法修复错误;需要类、接口(interface)或枚举。 274062

php - 来自 REST 客户端的 NetBeans 调试 session

java - 在 net beans 中添加新元素后对数组列表中的元素求和

ssl - ngx_slab_alloc() 失败 : no memory in SSL session shared cache "le_nginx_SSL"

spring-mvc - 错误页面在tomcat中正常工作,但在weblogic中工作不正常