protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Globals.MAIL_USERNAME, Globals.MAIL_PASSWORD);
}});
这样来写的话.就会出现题目上出现的错误,那是因为:
Session.getDefaultInstance()是获得一个默认的共享session,而创建一个session可以使用Session.getInstance()。
所以将程序改写成这样就OK了.
Properties props = System.getProperties();
props.put("mail.host",Globals.MAIL_SERVER);
props.put("mail.smtp.auth","true");
Session session1 = Session.getInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Globals.MAIL_USERNAME, Globals.MAIL_PASSWORD);
}});