在用javaMail API发送邮件时,会出现第一次发送成功,之后就一直失败的问题 在网上论坛上找到问题的解决方法: ====原程序使用getDefaultInstance方法会出错 Session session1 = Session.getDefaultInstance (props, new Authenticator(){
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); }});