……
Session sess = Session.getInstance(props, (Authenticator) new MailAuth(userid, passwd));
……
Transport trans = sess.getTransport("smtp");
Transport.send(mess);
trans.close();//--------------------
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuth extends Authenticator {
private String mUser;
private String mPass;
public MailAuth() {
}
public MailAuth(String username, String password) {
super();
mUser = username;
mPass = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser, mPass);
}
}
Session sess = Session.getInstance(props, (Authenticator) new MailAuth(userid, passwd));
……
Transport trans = sess.getTransport("smtp");
Transport.send(mess);
trans.close();//--------------------
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuth extends Authenticator {
private String mUser;
private String mPass;
public MailAuth() {
}
public MailAuth(String username, String password) {
super();
mUser = username;
mPass = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser, mPass);
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.*;
/**
* @author wjg
*
* 发送邮件简单类
*/
public class SendMail {
String host; //smtp服务器地址
String from; //发送邮箱地址
String username = null; //smtp用户名
String password = null; //smtp用户密码
public void setHost(String host) {
this.host = host;
}
public String getHost() {
return this.host;
}
public void setFrom(String from) {
this.from = from;
}
public String getFrom() {
return this.from;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return this.username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
/**
* 发送邮件到指定邮箱;调用前要设置host属性,
* 需要验证则设置用户名和密码,不支持附件
*
* @param sendto 发送目标邮箱
* @param subject //邮件主题
* @param content //邮件内容,可以含html
*/
public void sendMail(String sendto, String subject, String content) {
Authenticator mailAuth = null;
Properties props = new Properties();
props.put("mail.smtp.host", host);
if (this.username != null) {
mailAuth = new EmailAuthenticator(username, password);
props.put("mail.smtp.auth", "true");
}
Session session = Session.getInstance(props, mailAuth);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
sendto));
message.setSubject(subject);
message.setSentDate(new Date());
message.setContent(content, "text/html");
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
package com.wjg.email;
import javax.mail.Authenticator;
import javax.mail.*;
public class EmailAuthenticator
extends Authenticator {
String username = null;
String password = null;
public EmailAuthenticator() {
super();
}
public EmailAuthenticator(String username,String password) {
super();
this.username=username;
this.password=password;
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
楼主参考一下