package com.gdie.gdsafety.webapp.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.
internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.gdie.gdsafety.webapp.common.PropManager;
public class MailSender {
/**
* 发送单个邮件
*
* @throws Exception
*/
public void sendMail() throws Exception {
Properties props = new Properties();// 创建属性对象
props.put("mail.smtp.host", getHost());// 设置smtp服务器地址
props.put("mail.smtp.auth", "true");// 设置服务器smtp需要验证
Session session = Session.getInstance(props, null);// 创建新邮件并群发
MimeMessage message = new MimeMessage(session);// 创建过程对象
message.setFrom(new InternetAddress(getFromAddr()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(getToAddr()));
message.setSubject(getTitle());// 设置主题
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");// 设置信件内容
multipart.addBodyPart(contentPart);
if (getAttachPath() != null && getAttachName() != null) {
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
// BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GBK?B?" + getAttachName().getBytes()
+ "?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, getUsername(), getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 群发邮件
*
* @throws Exception
*/
public void sendMails() throws Exception {
Properties props = new Properties();// 创建属性对象
props.put("mail.smtp.host", getHost());// 设置smtp服务器地址
props.put("mail.smtp.auth", "true");// 设置服务器smtp需要验证
Session session = Session.getInstance(props, null);// 创建新邮件并群发
MimeMessage message = new MimeMessage(session);// 创建过程对象
message.setFrom(new InternetAddress(getFromAddr()));// 设置
发送邮件地址
message.setSentDate(new Date());// 设置时间
ArrayList<String> list = this.getMutliTo();
InternetAddress[] address = new InternetAddress[list.size()]; // 群发地址
for (int i=0;i<list.size();i++) { // 循环发送
address[i] = new InternetAddress(list.get(i));
}
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(getTitle());// 设置主题
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");// 设置信件内容
multipart.addBodyPart(contentPart);
if (getAttachPath() != null && getAttachName() != null) {
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
// BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GBK?B?" + getAttachName().getBytes()
+ "?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, getUsername(), getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public ArrayList<String> getMutliTo() {
return MutliTo;
}
public void setMutliTo(ArrayList<String> mutliTo) {
MutliTo = mutliTo;
}
public String getSendtext() {
return sendtext;
}
public void setSendtext(String sendtext) {
this.sendtext = sendtext;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFromAddr() {
return fromAddr;
}
public void setFromAddr(String fromAddr) {
this.fromAddr = fromAddr;
}
public String getToAddr() {
return toAddr;
}
public void setToAddr(String toAddr) {
this.toAddr = toAddr;
}
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 getAttachPath() {
return attachPath;
}
public void setAttachPath(String attachPath) {
this.attachPath = attachPath;
}
public String getAttachName() {
return attachName;
}
public void setAttachName(String attachName) {
this.attachName = attachName;
}
public MailSender(String host, String fromAddr, String toAddr,
String username, String password, String title, String attachPath,
String attachName, String sendtext, ArrayList<String> mutliTo) {
super();
this.host = host;
this.fromAddr = fromAddr;
this.toAddr = toAddr;
this.username = username;
this.password = password;
this.title = title;
this.attachPath = attachPath;
this.attachName = attachName;
this.sendtext = sendtext;
MutliTo = mutliTo;
}
private String host = null; // 邮件服务器
private String fromAddr = null; // 发送邮件地址
private String toAddr = null; // 接收邮件地址
private String username = null; // 发送邮件用户名
private String password = null; // 发送邮件密码
private String title = null; // 邮件标题
private String attachPath = null; // 邮件附件路径
private String attachName = null; // 邮件附件名
private String sendtext = null; // 邮件内容
private ArrayList<String> MutliTo = null; // 群发用户
public MailSender() {
super();
}
public static void main(String[] args) {
// ArrayList<String> mutlito = new ArrayList<String>();
// mutlito.add("liangbinny@126.com");
// mutlito.add("wenbin@gdie.com");
// mutlito.add("285203528@qq.com");
// MailSender mail = new MailSender("smtp.126.com", "liangbinny@126.com","wenbin@gdie.com", "liangbinny", "*******", "测试", null,null, "<table border=1><tr><td><a href='http://www.baidu.com'>百度</a></td><td>测试邮件发送是否成功!</td></tr></table>", mutlito);
// try {
// mail.sendMails();
// System.out.println("send ok!!!");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.println("ERROR!!!");
// }
PropManager pm = new PropManager();
String mailhost = pm.getMailhost(); //发送服务器
String mailsender = pm.getMailsender(); //发送者邮箱
String mailuser = pm.getMailuser(); //发送者用户名
String mailpassword = pm.getMailpassword(); //发送者密码
String mailtitle = pm.getMailtitle();
MailSender ms = null;
ms = new MailSender();
ms.setHost(mailhost);
ms.setFromAddr(mailsender);
ms.setUsername(mailuser);
ms.setPassword(mailpassword);
ms.setTitle(mailtitle);
ms.setSendtext("测试");
ms.setToAddr("liangbinny@126.com");
try {
ms.sendMail();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}