public class MyAuthenticator extends Authenticator {
public MyAuthenticator() {
super();
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
}
调用:
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Setup mail server
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
......................