日期:2014-05-18  浏览次数:20820 次

帮我分析下为什么这个javamail程序错误密码也能connect上去
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
public class Connect {
public  boolean islegal (String user,String password,String domain)throws Exception {
Transport t=null;
Properties props =new  Properties();
String servername="smtp."+domain.replace("@","");
String username=user+domain;
p(servername);
p(user+domain);
p(password);
props.setProperty("mail.smtp.host", servername);
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "25");
Session s=Session.getDefaultInstance(props, new MailAuthenticators(username, password));
//Session s=Session.getDefaultInstance(props);
 t=s.getTransport();
 p("connecting.....");
 t.connect();
 p("Successful");
 t.close();
return true;
   
}
    
public static void p(Object o){
System.out.println(o);
}
public static void main(String argsp[]) throws Exception {
p(new Connect().islegal("用户名","正确的密码","@163.com"));
p(new Connect().islegal("用户名","错误的密码","@163.com"));
}
}
class MailAuthenticators extends Authenticator {
private String userName;
private String password;
public MailAuthenticators(String userName, String password) {
   super();
   this.userName = userName;
   this.password = password;
}
@Override
protected PasswordAuthentication  getPasswordAuthentication() {
   return new PasswordAuthentication(userName, password);
}
}
第一次用正确的密码连接,第二次用错误密码连接,结果没报错,请问是怎么回事,是bug吗?
两次打印出来都是true,按理,第二次用错误的密码应该会有一个exception结果没有,这个怎么处理
现在在做一个页面,如果这样的话,客户第一次登录后,第二次用一个错误的密码也能登录成功,非常费解,希望有人可以指教一下
javamail

------解决方案--------------------
Session.getDefaultInstance()这个方法是返回默认的Session实例,如果没有创建实例的话则创建一个新的实例,第二次调用时会返回第一次创建的实例,所以第二个验证根本就没有执行,也就没有异常。

另外Connect.islegal()只有一个return true;所以执行两次都输出true