您需要实施一个自定义
Authenticator
import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;class GMailAuthenticator extends Authenticator { String user; String pw; public GMailAuthenticator (String username, String password) { super(); this.user = username; this.pw = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, pw); }}现在在
Session
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));
另请查看JavaMail常见问题解答



