2013-03-02 146 views
0

如何驗證電子郵件(Yahoo,Gmail,Hotmail和AOL)的用戶名和密碼? 哪裏應該把認真的auth電子郵件?Java - 如何驗證用戶名和密碼

這是我的代碼

public class AuthMail 
{ 
    public static void authEmail() 
    { 
     if(LoginDialog.t_Email.getText().isEmpty()) 
     { 
      Util.printErrorMessage("Plase Enter an Email Address"); 
      JOptionPane.showMessageDialog(null, "Plase Enter an Email Address", "Error", JOptionPane.ERROR_MESSAGE); 
     } 
     else if(LoginDialog.p_Password.getText().isEmpty()) 
     { 
      System.out.println("Plase Enter Your Password"); 
      JOptionPane.showMessageDialog(null, "Plase Enter Your Password", "Error", JOptionPane.ERROR_MESSAGE); 
     } 
     else if(Util.getMailType().equals("GMail")) 
     { 
      System.out.println("Logging..."); 
      System.out.println("Attempting connection to GMail..."); 
     } 
     else if(Util.getMailType().equals("Yahoo")) 
     { 
      System.out.println("Logging..."); 
      System.out.println("Attempting connection to Yahoo..."); 
     } 
     else if(Util.getMailType().equals("Yahoo")) 
     { 
      System.out.println("Logging..."); 
      System.out.println("Attempting connection to Hotmail..."); 
     } 
     else if(Util.getMailType().equals("AOL")) 
     { 
      System.out.println("Logging..."); 
      System.out.println("Attempting connection to Aol..."); 
     } 
     else if(Util.getMailType().equals("Unknown")) 
     { 
      System.out.println("Mail Type Unknown"); 
      int var1 = JOptionPane.showConfirmDialog(null, "Email Type Unknown. Did you want to try again?", "Error", JOptionPane.YES_NO_OPTION); 
      if(var1 == JOptionPane.NO_OPTION) 
      { 
       System.exit(0); 
      } 
     } 
    } 

}

注:Util.class是由自己創造。

+0

一件事,你需要JInputDialog框而不是JMessageDialog來請求輸入。 – 2013-03-02 03:48:05

+1

@lonnez No.我使用帶有JTextField和JPasswordField的JPanel的JFrame – 2013-03-02 03:50:21

+0

你基本上想要檢查用戶是否輸入了正確的用戶名和密碼。 – kaysush 2013-03-02 03:53:19

回答

1

使用接受主機,用戶和密碼的Store.connect方法。你正在收集用戶名和密碼。主機名將在您的「if」的每個分支中有所不同。您將在JavaMail FAQ中找到常見服務的主機名。

+0

如果連接失敗,connect()方法會返回什麼? – 2013-03-04 00:49:34

+0

按照[javadocs](http://javamail.kenai.com/nonav/javadocs/javax/mail/Service.html#connect(java.lang.String,%20java.lang.String,%20java.lang .String)),connect會返回void,但會拋出異常(如AuthenticationFailedException)來指示身份驗證問題,並通過MessagingException指示其他問題(如連接問題)。 – 2013-03-04 19:34:33

+0

那麼,它應該工作,如果我使用try()和catch()方法? – 2013-03-07 00:36:02