已经说过好多遍了
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
import javax.mail.*;public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
import javax.mail.*;public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
import java.util.*;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;/**
* <p>Title: Email</p>
* <p>Description: 发送Email</p>
*/
public class Mail{
public boolean sendMail(String subject,String body,String to){
try {
SmtpAuth sa=new SmtpAuth(); // 密码验证
sa.getuserinfo(你的用户名,口令);
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);//调试信息
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(你的EMAIL地址,显示名字));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args){
Mail m=new Mail();
m.sendMail("test","test javamail api",目标EMAIL地址);
}
}这是认证函数:
import java.util.Date;
import java.util.*;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;/**
* <p>Title: Email身份验证</p>
* <p>Description: Email身份验证</p>
*/ public class SmtpAuth extends javax.mail.Authenticator{ //SMTP身份验证
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
} protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
你把上面的程序稍微做一下改动,放到你的JSP页面里就OK了!