當今mail服務器大多都是通過認證才能發信的,現在的網上介紹javamail發信的文章都沒有深入到有關認證的方面,除非自己裝一個open
relay的mail服務器,但是這樣有很危險,本人根據自己工作中用的javamail的方法說一下自己的用法,不對的地方請大家多指教.
首先設置屬性Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");注意的是此處必須加上true要不然stmp連接的時候不會認證
用Authenticator寫認證類下面是本人的認證類
package org.xxx;
import javax.mail.*;
import javax.mail.internet.*;
public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
認證類寫完后,在發信的程序中加上
PasswordAuthentication pop = popAuthenticator.performCheck(username,password);
Session mysession=Session.getInstance(props,popAuthenticator);
mailsession加的popAuthenticator
其他的方法和javamail發信的用法相似,在此不累述。
http://www.cn-java.com/target/news.php?news_id=1511