java中的hotmail登录错误(IDE:Netbeans) [英] hotmail login error in java (IDE: Netbeans)

查看:131
本文介绍了java中的hotmail登录错误(IDE:Netbeans)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

文章将解释为什么显然可以登录到您的hotmail帐户登录到gmail或yahoo。其实你登录到刚刚做的一样的帐户。事情是你使用会话#getDefaultInstance()而不是会话#getInstance()

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():


Session.getDefaultInstance 方法创建一个新的Session 第一个
time
它被调用,使用传递的属性。 随后的
调用将返回原始会话,并忽略
传入的任何属性。
如果要创建具有不同
属性的不同会话, 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帐户,但您应该发布异常堆栈跟踪,以便我们可以帮助你。

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.

更新:

根据此堆栈跟踪: / p>

Based on this stack trace:

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

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

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


选择 TLS
外发服务器(SMTP)下使用以下类型的加密连接,然后单击确定。注意出站服务器
(SMTP)框应设置为端口25.如果端口25在
网络或您的ISP中被阻止,则可以将SMTP端口设置为587。

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.

注意:配置Outlook以连接到MSN电子邮件帐户

您只应在尝试登录时添加以下代码行到您的代码您的Hotmail帐户:

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

还不要忘记替换 Session.getDefaultInstance Session.getInstance ,如上所述。

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

这篇关于java中的hotmail登录错误(IDE:Netbeans)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆