邮件实体类 MailInfo
package org.xfl.jmail;
import java.util.Properties;
public class MailInfo {
private String host;// 邮件服务器域名或IP
private String from;// 发件人
private String port;
private String to;// 收件人
private String cc;// 抄送人
private String username;// 发件人用户名
private String password;// 发件人密码
private String title;// 邮件的主题
private String content;// 邮件的内容
private boolean validate = false;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public Properties getProperty() {
Properties prop = new Properties();
prop.put("mail.smtp.host", this.host);
prop.put("mail.smtp.port", this.port);
prop.put("mail.smtp.auth", validate ? "true" : "false");
return prop;
}
}
自定义验证类MyAuthenticator
package org.xfl.jmail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
发送器类
package org.xfl.jmail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSender {
protected boolean sendMail(MailInfo mailInfo) {
Properties prop = mailInfo.getProperty();
MyAuthenticator auth = null;
if (mailInfo.isValidate()) {
auth = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(prop, auth);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFrom());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getTo());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getTitle());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
测试:
package org.xfl.jmail;
public class JavaMailMain {
private static String user = "****@sina.com";
private static String pwd = "****";
public static void main(String[] args) {
MailInfo mail = new MailInfo();
mail.setFrom("****@sina.com");
mail.setTo("****@qq.com");
mail.setTitle("Java Mail test");
mail.setContent("hello world");
mail.setUsername(user);
mail.setPassword(pwd);
mail.setHost("smtp.sina.com");
mail.setPort("25");
mail.setValidate(true);
MailSender jmail = new MailSender();
if (jmail.sendMail(mail)) {
System.out.println("ok");
}
else {
System.out.println("failure");
}
}
}