package com.lixx.mail;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;
import com.lixx.anaylize.DBUtil;
import com.sun.mail.util.MimeUtil;
/**
* 创建内含附件、图文并茂的邮件
* @author haolloyin
*/
public class WithAttachmentMessage {
private String mail_body_template = "<div style='background:#DEDEBE'>各位领导、同仁:<br>"+
" 大家好!<br>"+
" 原太平OA系统已经停止使用,其中的OA公文数据已导入到我司自行开发的简易公文查询系统,使用方法如下:<br>"+
" 地址:http://oldoa.minanins.com 或 http://10.192.21.87 <br>"+
" 用户名:username<br>"+
" 初始密码:password<br>"+
" 如果出现用户登不上的问题,请及时与信息技术部李星星联系,联系方式如下:<br>"+
" 电话: 075582510020<br> 邮箱: xing-x@minanins.com <br> 手机: 15220131047<br>"+
" 感谢大家对IT工作的大力支持,谢谢!<br><br> <font size='5' color='#642100'>民安财产保险有限公司信息技术部</font></div>";
private String mail_body = "";
private String mail_to = "";
public String getMail_body() {
return mail_body;
}
public void setMail_body(String mailBody) {
mail_body = mailBody;
}
public String getMail_to() {
return mail_to;
}
public void setMail_to(String mailTo) {
mail_to = mailTo;
}
/**
* 根据传入的文件路径创建附件并返回
*/
public MimeBodyPart createAttachment(String fileName) throws Exception {
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName(MimeUtility.encodeText(fds.getName()));
return attachmentPart;
}
/**
* 根据传入的邮件正文body和文件路径创建图文并茂的正文部分
*/
public MimeBodyPart createContent(String body, String fileName)
throws Exception {
// 用于保存最终正文部分
MimeBodyPart contentBody = new MimeBodyPart();
// 用于组合文本和图片,"related"型的MimeMultipart对象
MimeMultipart contentMulti = new MimeMultipart("related");
// 正文的文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
contentMulti.addBodyPart(textBody);
// 正文的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("logo_jpg");
contentMulti.addBodyPart(jpgBody);
// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
contentBody.setContent(contentMulti);
return contentBody;
}
public MimeBodyPart createContent(String body) throws Exception {
// 用于保存最终正文部分
MimeBodyPart contentBody = new MimeBodyPart();
// 用于组合文本和图片,"related"型的MimeMultipart对象
MimeMultipart contentMulti = new MimeMultipart("related");
// 正文的文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
contentMulti.addBodyPart(textBody);
// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
contentBody.setContent(contentMulti);
return contentBody;
}
/**
* 根据传入的 Seesion 对象创建混合型的 MIME消息
*/
public MimeMessage createMessage(Session session) throws Exception {
String from = "xingx-li@*******.com";
String to = this.getMail_to(); //shib_zhong@minanins.com
String subject = "关于使用OA系统密码的邮件";
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
// 创建邮件的各个 MimeBodyPart 部分
MimeBodyPart attachment01 = createAttachment("D:\\minganwendang\\E盘\\技术资料\\OA查询系统\\OA查询系统详细操作手册.doc");
// MimeBodyPart attachment02 = createAttachment("D:\\minganwendang\\E盘\\技术资料\\OA查询系统\\OA查询系统详细操作手册.doc");
MimeBodyPart content = createContent(this.getMail_body());
// 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
MimeMultipart allPart = new MimeMultipart("mixed");
allPart.addBodyPart(attachment01);
// allPart.addBodyPart(attachment02);
allPart.addBodyPart(content);
// 将上面混合型的 MimeMultipart 对象作为邮件内容并保存
msg.setContent(allPart);
msg.saveChanges();
return msg;
}
/**
* 用来进行服务器对用户的认证
*/
public class Email_Autherticator extends Authenticator
{
private String username="****";
private String password="*****";
public Email_Autherticator()
{
super();
}
public Email_Autherticator(String user, String pwd)
{
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
// 测试生成邮件
public static void main(String[] args) throws Exception {
WithAttachmentMessage mail = new WithAttachmentMessage();
Properties props = new Properties(); // 获取系统环境
Authenticator auth = mail.new Email_Autherticator(); // 进行邮件服务器用户认证
props.put("mail.smtp.host", "smtp.****.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,auth);
Connection conn = null;
String str = "select oa_email,oa_password from oa_user t where oa_email is not null ";
try {
conn = DBUtil.getInstance().getConnection();
conn.setAutoCommit(false);
Statement st = conn.createStatement() ;
PreparedStatement ps = conn.prepareStatement("update oa_user set oa_password=?,OA_PASSWORD_BAK=? where oa_email=?");
ResultSet rs = st.executeQuery(str);
while(rs.next()){
String tempEmail = rs.getString(1);
/**
* 更新密码
*/
String password = TestHnair.randomPassword("");
String ser_pass = MD5Util.getMD5Str(password);
ps.setString(1, ser_pass);
ps.setString(2,rs.getString(2));
ps.setString(3, tempEmail);
ps.addBatch();
System.out.println(tempEmail+" "+password+" "+ser_pass);
/**
* 发送邮件
*/
mail.setMail_to(tempEmail);
mail.setMail_body(
mail.mail_body_template
.replace("username", tempEmail.subSequence(0, tempEmail.indexOf("@")))
.replace("password", password));
MimeMessage message = mail.createMessage(session);
//message.writeTo(new FileOutputStream("withAttachmentMail.eml"));
Transport.send(message);
}
ps.executeBatch() ;
conn.commit();
ps.close();
conn.close();
System.out.println("send ok!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
conn.rollback();
}finally{
if(!conn.isClosed()){
conn.close();
}
}
}
}