首先是繼承自javax.mail.Authenticator的一個具體類。getPasswordAuthentication()方法也就是構建一個PasswordAuthentication對象并返回,有點費解JAVA Mail這樣的設計意圖,可能是javax.mail.Authenticator為我們提供了附加的保證安全的驗證措施吧。
代碼如下package com.mzule.simplemail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 服務器郵箱登錄驗證
*
* @author MZULE
*
*/
public class MailAuthenticator extends Authenticator {
/**
* 用戶名(登錄郵箱)
*/
private String username;
/**
* 密碼
*/
private String password;
/**
* 初始化郵箱和密碼
*
* @param username 郵箱
* @param password 密碼
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
新聞熱點
疑難解答