package com.***.***.modules.user.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import com.***.***.fwk.base.***Action;
import com.***.***.modules.system.system.domain.ParameterBO;
import com.***.***.modules.user.domain.UserBO;
import com.***.***.modules.user.domain.UserBean;
import com.***.***.util.PropertyReader;
import com.***.exception.ParameterException;
import com.***.exception.RecordNotFoundException;
import com.***.util.ExceptionHandle;
/**
* <p>Title: UserDeleteAction/p>
* <p>Description:通过邮件发送通告 </p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: *** co.ltd</p>
* @author daizh
* @version 1.0
*/
public class AdviceAgreementAction extends ***Action {
public ActionForward enforce(ActionMapping actionMapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse reponse) throws RecordNotFoundException {
UserForm userForm = (UserForm) form;
UserBean userBean = getUserBean(request);
String centerDeptName= userForm.getDeptName();
String subject = userForm.getMailTitle();
String description =userForm.getDescription();
UserBO userBO = new UserBO();
try {
//获取部门下的子部门集合
Collection deptsCol = userBO.getAlldeptByDeptID(userForm.getDeptID());
for (Iterator i = deptsCol.iterator();i.hasNext();) {
HashMap hm = (HashMap)i.next();
String deptID = hm.get("DEPTID").toString();
String deptName = hm.get("DEPTNAME").toString();
//获取当前部门包括子部门下的所有用户ID
Collection sendingAddressCol_arg = userBO.getMailListByDeptIDArg(deptID);
System.out.println("sendingAddressCol_arg.size(): "+sendingAddressCol_arg.size());
Collection sendingAddressCol_not_arg = userBO.getMailListByDeptIDNotArg(deptID);
System.out.println("sendingAddressCol_not_arg.size()"+sendingAddressCol_arg.size());
StringBuffer sbMailList_arg = new StringBuffer("");
if( (sendingAddressCol_arg == null || sendingAddressCol_arg.size()==0) && (sendingAddressCol_not_arg == null || sendingAddressCol_not_arg.size()==0))
continue;
boolean first_arg = true;
for (Iterator j = sendingAddressCol_arg.iterator();j.hasNext();) {
HashMap hm_arg = (HashMap)j.next();
if(first_arg){
first_arg = false;
}
else{
sbMailList_arg.append(",");
}
String mailTo = hm_arg.get("MAILTO").toString();
mailTo = mailTo.substring(mailTo.indexOf('<'));
sbMailList_arg.append(mailTo);
}
System.out.println("sbMailList_arg: "+sbMailList_arg);
StringBuffer sbMailList_not_arg = new StringBuffer("");
// if(sendingAddressCol_not_arg == null || sendingAddressCol_not_arg.size()==0)
// return actionMapping.findForward("success");
boolean first_not_arg = true;
for (Iterator k = sendingAddressCol_not_arg.iterator();k.hasNext();) {
HashMap hm_not_arg = (HashMap)k.next();
if(first_not_arg){
first_not_arg = false;
}
else{
sbMailList_not_arg.append(",");
}
String mailTo = hm_not_arg.get("MAILTO").toString();
mailTo = mailTo.substring(mailTo.indexOf('<'));
sbMailList_not_arg.append(mailTo);
}
System.out.println("sbMailList_not_arg: "+sbMailList_not_arg);
List list_inc_stat = userBO.listDeptIncSubAgreementDetail(userForm.getDeptID());
List list_stat = userBO.listEachDeptAgreementDetail(userForm.getDeptID());
System.out.println("list_stat: "+list_stat.size());
// List list_not_agr_user = userBO.listDeptNotAgreementUser(deptID);
// System.out.println("the record count"+list_not_agr_user.size());
List list_user = userBO.listDeptUser(deptID);
System.out.println("the record count"+list_user.size());
String tr2="";
String record2="";
for(int n=0; n<list_user.size();n++){
Map map = (Map)list_user.get(n);
String userID = map.get("USERID").toString();
String userName = map.get("USERNAME").toString();
String fullName = map.get("FULLNAME").toString();
String userType = map.get("USERTYPE").toString();
String agreement = map.get("AGREEMENT").toString();
if(agreement.equals("未签署")){
agreement ="<font color='red' size='2'>"+agreement+"</font>";
}else{
agreement ="<font size='2'>"+agreement+"</font>";
}
//String mobile = map.get("MOBILE")==null?"":map.get("MOBILE").toString();
//String email = map.get("EMAIL")==null?"":map.get("EMAIL").toString();
String partenerName = map.get("PARTENERNAME")==null?" ":map.get("PARTENERNAME").toString();
String contacter = map.get("CONTACTER")==null?" ":map.get("CONTACTER").toString();
record2 ="<tr>"
+"<td width='114'><font size='2'>"+fullName+"</font></td>"
+"<td width='114'><font size='2'>"+userType+"</font></td>"
+"<td width='114'>"+agreement+"</td>"
+"<td width='114'><font size='2'>"+contacter+"</font></td>"
+"<td width='505'><font size='2'>"+partenerName+"</font></td>"
+"</tr>";
tr2 = tr2 + record2;
}
String tr1="";
String record0="";
for(int m=0; m<list_inc_stat.size();m++){
Map map = (Map)list_inc_stat.get(m);
String vDeptName = map.get("DEPTNAME").toString();
String total = map.get("TOTAL").toString();
String total_agr = map.get("TOTAL_AGR").toString();
String total_per = map.get("TOTAL_PER").toString();
String total_emp = map.get("TOTAL_EMP").toString();
String total_arg_emp = map.get("TOTAL_ARG_EMP").toString();
String arg_emp_per = map.get("ARG_EMP_PER").toString();
String total_dw = map.get("TOTAL_DW").toString();
String total_arg_dw = map.get("TOTAL_ARG_DW").toString();
String arg_dw_per = map.get("ARG_DW_PER").toString();
record0 ="<tr>"
+"<td width='300'><font size='2'>"+vDeptName+"(含室)"+"</font></td>"
+"<td width='220'><font size='2'>"+total+"</font></td>"
+"<td width='80'><font size='2'>"+total_agr+"</font></td>"
+"<td width='80'><font size='2'>"+total_per+"</font></td>"
+"<td width='80'><font size='2'>"+total_emp+"</font></td>"
+"<td width='80'><font size='2'>"+total_arg_emp+"</font></td>"
+"<td width='80'><font size='2'>"+arg_emp_per+"</font></td>"
+"<td width='80'><font size='2'>"+total_dw+"</font></td>"
+"<td width='80'><font size='2'>"+total_arg_dw+"</font></td>"
+"<td width='80'><font size='2'>"+arg_dw_per+"</font></td>"
+"</tr>";
tr1 = tr1 + record0;
}
String record1="";
for(int t=0; t<list_stat.size();t++){
Map map = (Map)list_stat.get(t);
String v_deptName = map.get("DEPTNAME").toString();
String total = map.get("TOTAL").toString();
String total_agr = map.get("TOTAL_AGR").toString();
String total_per = map.get("TOTAL_PER").toString()+"%";
String total_emp = map.get("TOTAL_EMP").toString();
String total_arg_emp = map.get("TOTAL_ARG_EMP").toString();
String arg_emp_per = map.get("ARG_EMP_PER").toString()+"%";
String total_dw = map.get("TOTAL_DW").toString();
String total_arg_dw = map.get("TOTAL_ARG_DW").toString();
String arg_dw_per = map.get("ARG_DW_PER").toString()+"%";
record1 ="<tr>"
+"<td width='300'><font size='2'>"+v_deptName+"</font></td>"
+"<td width='220'><font size='2'>"+total+"</font></td>"
+"<td width='80'><font size='2'>"+total_agr+"</font></td>"
+"<td width='80'><font size='2'>"+total_per+"</font></td>"
+"<td width='80'><font size='2'>"+total_emp+"</font></td>"
+"<td width='80'><font size='2'>"+total_arg_emp+"</font></td>"
+"<td width='80'><font size='2'>"+arg_emp_per+"</font></td>"
+"<td width='80'><font size='2'>"+total_dw+"</font></td>"
+"<td width='80'><font size='2'>"+total_arg_dw+"</font></td>"
+"<td width='80'><font size='2'>"+arg_dw_per+"</font></td>"
+"</tr>";
tr1 = tr1 + record1;
}
StringBuffer ccList = new StringBuffer("");
if( userForm.getCopyMakeId()!=null && !"".equals(userForm.getCopyMakeId().trim())){
String[] ccTo = userForm.getCopyMakeId().split(",");
for(int j= 0;j<ccTo.length;j++)
{
if( j == ccTo.length -1)
{
ccList.append(ccTo[j].substring(ccTo[j].indexOf('<')));
break;
}
ccList.append(ccTo[j].substring(ccTo[j].indexOf('<'))).append(",");
}
}
//上传文件
String dir = servlet.getServletContext().getRealPath("/upload");//获取存储图片的地址
MultipartRequestHandler mrh = userForm.getMultipartRequestHandler();
Hashtable ht = mrh.getFileElements();
Enumeration files = ht.elements();
List urllist = new ArrayList();
while (files.hasMoreElements()) {
FormFile formFile= (FormFile)files.nextElement();
String fileName = formFile.getFileName();
String url = dir+"/" + fileName;//获取文件的绝对路径
if(!fileName.trim().equals("")){
InputStream in = formFile.getInputStream();
upLoad(in ,url);
urllist.add(url);
}
//System.out.println("url="+url);
}
System.out.println(tr1);
System.out.println(tr2);
// UserNotify.getInstance().mailNotify(userBean,sbMailList_arg.toString(),ccList.toString(),userForm.getMailTitle(),userForm.getDescription(),argData,userBean.getEmail() ,urllist);
// UserNotify.getInstance().mailNotify(userBean,sbMailList_not_arg.toString(),ccList.toString(),userForm.getMailTitle(),userForm.getDescription(),notArgData,userBean.getEmail() ,urllist);
AdviceAgreementAction adviceAgreementAction = new AdviceAgreementAction();
if(sbMailList_arg!=null && !"".equals(sbMailList_arg) && sbMailList_not_arg!=null && !"".equals(sbMailList_not_arg)){
adviceAgreementAction.sendNoteByMail(sbMailList_arg.toString(), subject, description, centerDeptName, deptName, tr1, tr2, userBean);
adviceAgreementAction.sendNoteByMail(sbMailList_not_arg.toString(), subject, description, centerDeptName, deptName, tr1, tr2, userBean);
}
//adviceAgreementAction.sendNoteByMail("", "【***协议2.0签署】任务完成情况", "描述", userForm.getDeptName(), deptName, tr1, tr2, userBean);
deleteFile(urllist);
}
} catch (SQLException ex) {
String key =
"com.***.***.modules.user.action.SQLError";
new ExceptionHandle(request, key, ex);
return actionMapping.findForward("error");
} catch (IOException ex) {
String key =
"com.***.***.modules.user.action.IOError";
new ExceptionHandle(request, key, ex);
return actionMapping.findForward("error");
} catch (ParameterException ex) {
String key =
"com.***.***.modules.user.action.ParameterError";
new ExceptionHandle(request, key, ex);
return actionMapping.findForward("error");
}
// catch (UserNotifyException ex) {
// String key =
// "com.***.***.modules.user.action.UserNotifyError";
// new ExceptionHandle(request, key, ex);
// return actionMapping.findForward("error");
//
// }
return actionMapping.findForward("success");
}
private void upLoad(InputStream in, String url) {
//InputStream in = null;
OutputStream out = null;
try {
out = new FileOutputStream(url);
}
catch (IOException ex) {
}
//将文件输出到目标文件
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
while ( (bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
} catch (IOException ex) {
}
try {
out.close();
in.close();
} catch (IOException ex) {
}
}
/*
* 上传后再删除该文件.
*/
private void deleteFile(List urllist){
for (int i = 0; i < urllist.size(); i++) {
(new File((String)urllist.get(i))).delete();
}
}
private void sendNoteByMail(String addresses, String subject, String description, String belongCenterName, String belongdeptName, String tr1, String tr2, UserBean userBean_sender )throws SQLException,RecordNotFoundException, IOException{
if(belongCenterName==null || belongCenterName.trim().equals("")){
belongCenterName = belongdeptName;
}
//SMTP邮件服务器地址
String smtp_server_addr = ParameterBO.getParameterValueByName(ParameterBO.SMTP_SERVER_ADDR);
//邮件发送帐号
String smtp_account = ParameterBO.getParameterValueByName(ParameterBO.SMTP_ACCOUNT);
//邮件发送帐号密码
String smtp_password = ParameterBO.getParameterValueByName(ParameterBO.SMTP_PASSWORD);
//邮件发送者地址
String mail_sender = ParameterBO.getParameterValueByName(ParameterBO.MAIL_SENDER);
//邮件发送是否需要认证
String smtp_needauth = ParameterBO.getParameterValueByName(ParameterBO.SMTP_NEEDAUTH);
boolean boolean_smtp_needauth =smtp_needauth.equals("0")? false:true;
try{
Properties props=new Properties();
props.put("mail.smtp.host",smtp_server_addr);
props.put("mail.smtp.auth",String.valueOf(boolean_smtp_needauth));//需要身份验证
props.put("mail.dubug",String.valueOf(true));//显示调试信息,方便调试
PopupAuthenticator auth=new PopupAuthenticator(smtp_account, smtp_password);
Session session=Session.getInstance(props,auth);// 创建会话
session.setDebug(true);
MimeMessage message=new MimeMessage(session);//建立消息
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress(mail_sender); //发邮件的出发地(发件人的信箱)
message.setFrom(from);
//InternetAddress to=new InternetAddress(tto);//发邮件的目的地(收件人信箱)
//message.setRecipient(Message.RecipientType.TO,to);//收件人, 普通发送方式
String[] addressList = addresses.split(",");
boolean flag=true;
if (addressList!=null && addressList.length>0){
for(int i=0; i<addressList.length;i++){
String strReceiver = addressList[i].toString();
if(strReceiver!=null && !strReceiver.trim().equals("")){
System.out.println(strReceiver);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver));
}else{
flag=false;
}
}
}
// String strReceiver1 = new String("***@163.com");
// String strReceiver2 = new String("***@163.com");
// String strReceiver3 = new String("***@***.com.cn");
//
// message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver1));
// message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver2));
// message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver3));
message.setSubject(subject);//邮件主题
message.setSentDate(new Date());//发送日期
//给消息对象设置内容
BodyPart mbp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
PropertyReader propReader = new PropertyReader("/conf/NotifyConfig.properties");
String strLtimServiceUrl = propReader.getProperty("LTIM_SERVICE_URL");
String strClientSystemName = propReader.getProperty("CLIENT_SYSTEM_NAME");
String s_clientSystemName_link="网络安全**管理系统 <a href='http://127.0.0.1/'>http://127.0.0.1/</a>";
if(strClientSystemName!=null){
s_clientSystemName_link = new String(strClientSystemName.getBytes("ISO8859-1"),"GBK")+" "+strLtimServiceUrl+" ";
}
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = tempDate.format(new java.util.Date());
String contactor ="";
String contact_mail="";
String contactStr="";
if(userBean_sender.getUserName().trim().equals("wuyubin")){
contactor="***";
contact_mail="***@gd.***.com";
contactStr = contactor + " : "+contactStr;
}else{
contactor=userBean_sender.getFullName();
contact_mail=userBean_sender.getEmail();
//contactStr = contactor +" : "+ contact_mail +" 或 " +" *** : " + "***@gd.***.com";
contactStr = contactor +" : "+ contact_mail;
}
String content="<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><title>通告</title></head>"
+"<body bgcolor='#EDF8FF'><p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>本邮件是由中国***通信集团广东有限公司</font>"
+"<font face='Default Sans Serif,Verdana,Arial,Helvetica,sans-serif' size='2'><font face='Default Monospace,Courier New,Courier,monospace' size='2'>"
+"<a href='http://127.0.0.1/'>"+"网络安全**管理系统"+"</a>"
+"自动发出,请勿直接回复。</font></p>"
+"<p><font face='Default Monospace,Courier New,Courier,monospace' size='2'> "
+"如有疑问,请联系 "
+contactStr
+"</font></p></font><p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>注:实时数据可通过登录系统"
+ "<a href='http://127.0.0.1/'>http://127.0.0.1/</a>"
+"查看。</font></p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>"
+description
+"</font><p></p><font face='Default Sans Serif,Verdana,Arial,Helvetica,sans-serif' size='2'><p></p></font>"
+"<p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>--------------------以下内容发送范围【"
+ belongCenterName
+", 不含部门领导】--------------------</font><font face='Default Sans Serif,Verdana,Arial,Helvetica,sans-serif' size='2'><font face='Default Monospace,Courier New,Courier,monospace' size='2'><br>"
+"任务名称: "
+"***协议2.0签署"
+"<br>"
+"任务完成情况"
+"(截止至"
+datetime
+")</font></p>"
+"<table border='1' width='100%' id='table1'><tr>"
+"<td width='300'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>单位名称</font></td>"
+"<td width='220'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>总人数(员工+代维)</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成数</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成率</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>员工数</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成数</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成率</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>代维数</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成数</font></td>"
+"<td width='88'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成率</font></td>"
+"</tr>"
//+"<tr><td width='100'></td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td><td width='100'> </td></tr>"
+tr1
+"</table></font><p></p><p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>"
+"-----------------------------以下内容发送范围【"
+belongdeptName
+"】----------------------------<br>"
+"其中您所在的室("
+belongdeptName
+")"
+"【***协议2.0签署】任务完成情况"
+"</font></p><table border='1' width='81%' id='table2'>"
+"<tr><td width='112'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>姓名</font></td>"
+"<td><font face='Default Monospace,Courier New,Courier,monospace' size='3' color='#0000FF'>类型</font></td>"
+"<td width='111'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>完成情况</font></td>"
+"<td width='122'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>***联系人</font></td>"
+"<td width='605'><font face='Default Monospace,Courier New,Courier,monospace' size='2' color='#0000FF'>厂商</font></td></tr>"
+tr2
+"</table><p><font face='Default Monospace,Courier New,Courier,monospace' size='2'>"
+"感谢您对网络安全工作的支持,对尚未按要求完成网络安全任务【***协议2.0签署】的人员,请您友情提醒,谢谢! </font></p>"
+"</body></html>";
mbp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
//象(事实上可以存放多个)
mm.addBodyPart(mbp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(smtp_server_addr,smtp_account,smtp_password);//发邮件人帐户密码
if(flag!=false){
transport.sendMessage(message,message.getAllRecipients());
System.out.println("发送成功!");
}
transport.close();
}catch(MessagingException e){
e.printStackTrace();
System.out.println("发送失败!");
}
}
}
class PopupAuthenticator extends Authenticator{
private String username,password;
public PopupAuthenticator(String username,String password){
this.password=password;
this.username=username;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
}