分类:
2008-09-11 14:31:59
1.首先设置属性
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");//必须加上true要不然stmp连接的时候不会认证.
2.写认证类继承自Authenticator:
import javax.mail.*;
import javax.mail.internet.*;
public class MyAuthenticator extends Authenticator
{
String userName=null;
String password=null;
public MyAuthenticator()
{
}
public PasswordAuthentication performCheck(String userName,String password)
{
this.userName = userName;
this.password = password;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
}
3.在发信的程序中加上
MyAuthenticator myAuthenticator = new MyAuthenticator();
PasswordAuthentication my= myAuthenticator.performCheck(userName,password);
Session mailsession=Session.getInstance(props,myAuthenticator);