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 ;
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
+"自動發出,請勿直接回復。</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
+"查看。</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);
}
}