[英]Validating user email authentication
我正在提示我的應用程序用戶輸入電子郵件憑據。
用戶插入電子郵件並通過后,我要驗證帳戶。
我正在使用javax.mail 。 有什么方法可以驗證帳戶? 只確保憑據確實有效-否則我想顯示一個無效的用戶並傳遞消息。
也許有一些執行方法:
Transport.send(message);
並且在不發送任何內容的同時檢查身份驗證異常。
嗨,你可以做這樣的事情
public final static boolean validateEmail(CharSequence givenSeq) {
if (givenSeq!= null) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(givenSeq).matches();
} else {
return false;
}
}
您可以創建合理性檢查,例如(僅僅是示例,距離完成還很遠):
String email = "anonym.user@mail.com";
if (!email.contains("@") || !email.contains(".") || !(email.lastIndexOf(".") > email.indexOf("@")))
showError();
嘗試這個:
/**
* validate your email address format. Ex-xyz@abc.com
*/
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();
}
您可以創建傳輸對象並管理連接,以驗證用戶名/密碼是否有效。
Address[] to = InternetAddress.parse("me@localhost");
Transport t = session.getTransport(to[0]);
t.connect();
t.close();
如果要驗證用戶名/密碼和信封,只需創建一條消息,其中包含所有信封信息集,但不包含任何內容。 或創建具有惡意writeTo(OutputStream)方法的MimeMessage的子類。
MimeMessage msg = new MimeMessage(session);
Address[] from = InternetAddress.parse("me@localhost");
Address[] to = InternetAddress.parse("me@localhost");
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引發異常,因此不會發送任何實際的電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.