import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 邮件实体类,封装了有关发送 邮件的方法。
* @author xxxxxx
*
*/
public class EmailEntity {
private MimeMessage msg;
/**
* 初始化邮件实体
*
* @param fromDomin
* 发件人邮件服务器域名,如:163.com、qq.com
* @param userName
* 发件人用户名,如:qmail、20082345
* @param password
* 发件人密码
* @param toAddress
* 收件人邮箱地址,如:200712345@qq.com
*/
public EmailEntity(String fromDomin, String userName, String password) {
Properties p = new Properties();
//设置邮件发送服务器的地址
p.setProperty("mail.host", "smtp." + fromDomin);
//设置使用权限验证
p.setProperty("mail.smtp.auth", "true");
//设置用户身份验证凭据
Session ses = Session.getDefaultInstance(p, new MyAuthenticator(userName, password));
//ses.setDebug(true);//设置是否出现回显信息
//创建邮件实体
msg = new MimeMessage(ses);
try {
//设置发件人邮箱地址
msg.setFrom(new InternetAddress(userName + "@" + fromDomin));
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送消息
*
* @param title
* 邮件标题
* @param content
* 邮件正文
* @param type
* 正文的类型,如:text/html、text/plain
* @return
*/
public boolean sendMessage(String toAddress, String title, String content, String type) {
try {
//设置发件人邮箱地址
msg.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
//设置邮件发送日期
msg.setSentDate(new Date());
//设置邮件标题
msg.setSubject(title);
//设置邮件正文
msg.setContent(content, type);
//开始发送邮件
Transport.send(msg);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
/**
* 发送带附件的邮件
* @param toAddress 收件人的邮箱地址,如bob@126.com
* @param title 邮件标题
* @param content 邮件正文,包括附件
* @param type 邮件正文的类型
* @return
*/
public boolean sendEmailWithAttachment(String toAddress, String title, MimeMultipart content, String type) {
try {
msg.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
msg.setSentDate(new Date());
msg.setSubject(title);
msg.setContent(content);
Transport.send(msg);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
}
package com.test.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
//用户身份验证凭据类
class MyAuthenticator extends Authenticator {
private String _userName;
private String _password;
public MyAuthenticator(String userName,String password){
this._userName = userName;
this._password = password;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
//返回使用了用户名和密码的身份验证凭据
return new PasswordAuthentication(_userName, _password);
}
}
package com.test.mail;
public class main {
/**
* @param args
*/
public static void main(String[] args){
EmailEntity ee=new EmailEntity("163.com", "wang163", "password");
ee.sendMessage("xxxxxxxxx@qq.com", "title", "context", "text/html");
}
}