2014-04-07 94 views
0

我正在提示我的應用程序用戶輸入電子郵件憑據。驗證用戶電子郵件身份驗證

用戶插入電子郵件並通過後,我想驗證帳戶。

我正在使用javax.mail。有什麼方法可以驗證帳戶嗎?只確保憑據確實有效 - 否則我想顯示一個無效的用戶並傳遞消息。

也許某種方式來進行:

Transport.send(message); 

,檢查認證例外,而沒有發送任何。

回答

0

您可以創建真實性檢查類似(只是一個例子,遠離完成):

String email = "[email protected]mail.com"; 

if (!email.contains("@") || !email.contains(".") || !(email.lastIndexOf(".") > email.indexOf("@"))) 
    showError(); 
1

嗨,你可以做這樣的事情

public final static boolean validateEmail(CharSequence givenSeq) { 
    if (givenSeq!= null) { 
     return android.util.Patterns.EMAIL_ADDRESS.matcher(givenSeq).matches(); 
    } else { 
     return false; 
    } 
} 
0

試試這個:

/** 
* validate your email address format. [email protected] 
*/ 
public boolean emailValidator(String email) 
{ 
    Pattern pattern; 
    Matcher matcher; 
    final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    pattern = Pattern.compile(EMAIL_PATTERN); 
    matcher = pattern.matcher(email); 
    return matcher.matches(); 
} 
0

您可以創建傳輸對象並管理連接以驗證用戶名/密碼是否有效。

Address[] to = InternetAddress.parse("[email protected]"); 
Transport t = session.getTransport(to[0]); 
t.connect(); 
t.close(); 

如果您想驗證用戶名/密碼和信封,只需創建一個包含所有信封信息但沒有內容的信息。或者創建一個具有惡意writeTo(OutputStream)方法的MimeMessage的子類。

MimeMessage msg = new MimeMessage(session); 
    Address[] from = InternetAddress.parse("[email protected]"); 
    Address[] to = InternetAddress.parse("[email protected]"); 
    msg.addFrom(from); 
    msg.setRecipients(Message.RecipientType.TO, to); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(384); 
    msg.saveChanges(); 
    try { 
     msg.writeTo(out); 
     throw new AssertionError(); 
    } catch (MessagingException | IOException test) { 
     try { 
      Transport.send(msg); 
      throw new AssertionError(); 
     } catch (MessagingException | IOException expect) { 
      if (!exceptionEqual(test, expect)) { 
       //Notify the user.... 
      } 
     } 
    } 

這部作品的原因是,該內容被最後寫入,所以如果Transport.send使得它Message.writeTo那麼你知道不,你可以登錄到郵件服務器的一個疑問。此外,服務器會通知您是否接受信封信息。但是,僅僅因爲它接受了信封並不意味着交付將會成功。由於Message.writeTo引發異常,因此不會發送實際的電子郵件消息。

相關問題