Compose.aspx.cs
上传用户:cha0314
上传日期:2014-03-02
资源大小:12522k
文件大小:19k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using System.Configuration;
  13. using System.Data.SqlClient;
  14. using UDS.Components;
  15. namespace UDS.SubModule.UnitiveDocument.Mail
  16. {
  17. /// <summary>
  18. /// Compose 的摘要说明。
  19. /// </summary>
  20. public class Compose : System.Web.UI.Page
  21. {
  22. protected System.Web.UI.WebControls.Label lblSendTo;
  23. protected System.Web.UI.WebControls.Label lblSubject;
  24. protected System.Web.UI.WebControls.TextBox txtSubject;
  25. protected System.Web.UI.WebControls.Label lblBody;
  26. protected System.Web.UI.WebControls.TextBox txtBody;
  27. protected System.Web.UI.WebControls.Label lblImportance;
  28. protected System.Web.UI.WebControls.DropDownList listImportance;
  29. protected System.Web.UI.WebControls.Button btnSendMail;
  30. protected System.Web.UI.WebControls.Label lblAttachFile;
  31. protected System.Web.UI.WebControls.Label lblCcTo;
  32. protected System.Web.UI.WebControls.Label lblBccTo;
  33. protected System.Web.UI.HtmlControls.HtmlInputFile filecontrol1;
  34. protected static string Username;
  35. public string ClassID;
  36. public static string MailID;
  37. public string Action;
  38. public string SendTo="",CcTo="",BccTo="",SendToRealName="",CcToRealName="",BccToRealName="";
  39. protected System.Web.UI.WebControls.CheckBoxList cblistAttribute;
  40. protected System.Web.UI.WebControls.Button btnUpload;
  41. protected System.Web.UI.WebControls.ListBox listUp;
  42. protected System.Web.UI.WebControls.Button btnRemove;
  43. protected System.Web.UI.HtmlControls.HtmlInputFile filecontrol2;
  44. protected System.Web.UI.HtmlControls.HtmlInputFile File1;
  45. protected System.Web.UI.HtmlControls.HtmlInputFile File2;
  46. protected System.Web.UI.WebControls.CheckBox cbRemind;
  47. protected  ArrayList upattlist = new ArrayList();
  48. private void Page_Load(object sender, System.EventArgs e)
  49. {
  50. ClassID  = (Request.QueryString["ClassID"]!=null)?Request.QueryString["ClassID"].ToString():"";
  51. MailID  = (Request.QueryString["MailID"]!=null)?Request.QueryString["MailID"].ToString():"";
  52. HttpCookie UserCookie = Request.Cookies["Username"];
  53. Username = UserCookie.Value.ToString();
  54. if(!IsPostBack)
  55. {
  56. Session["upattlist"]=upattlist;
  57. //upattlist.Clear();
  58. // Action=1 回复 Action=2 转发 Action=3 指定收件人的寄信
  59. if (Request.QueryString["Action"]!=null)
  60. {
  61. MailID  = (Request.QueryString["MailID"]==null)?"":Request.QueryString["MailID"].ToString();
  62. if(Request.QueryString["Action"]=="1")
  63. ReplySet();  // 进行回复邮件设置
  64. if(Request.QueryString["Action"]=="2")
  65. ForwardSet();  // 进行转发邮件设置
  66. if(Request.QueryString["Action"]=="3")
  67. ReceiverSet();  // 进行发信人设置 
  68. }
  69. PopulateListView();
  70. }
  71. }
  72. #region 回复邮件设置
  73. /// <summary>
  74. /// 回复邮件设置
  75. /// </summary>
  76. private void ReplySet()
  77. {
  78. // 读取原邮件内容
  79. MailClass mailclass = new MailClass();
  80. SqlDataReader dataReader = null; 
  81. try
  82. {
  83. dataReader = mailclass.GetMailCompleteInfoDbreader(MailID);
  84. }
  85. catch
  86. {
  87. Server.Transfer("../../Error.aspx");
  88. }
  89. if(dataReader.Read())
  90. {
  91. string tmpStr  = "<br/>"+dataReader[7].ToString();
  92. tmpStr  = tmpStr.Replace("<br/>","rn>");
  93. this.txtSubject.Text = "Re:"+dataReader[4].ToString();
  94. SendToRealName       = dataReader[1].ToString()+",";
  95. SendTo  = dataReader[10].ToString()+",";
  96. this.txtBody.Text    = SendToRealName+"你好!nnnnnnn";
  97. this.txtBody.Text   += "======="+dataReader[3].ToString()+"你在来信中写道:"+"=======nn";
  98. this.txtBody.Text += tmpStr;
  99. }
  100. dataReader.Close();
  101. mailclass = null;
  102. }
  103. #endregion
  104. #region 转发邮件设置
  105. /// <summary>
  106. /// 转发邮件设置
  107. /// </summary>
  108. private void ForwardSet()
  109. {
  110. // 读取原邮件内容
  111. MailClass mailclass = new MailClass();
  112. SqlDataReader dataReader = null; 
  113.             try
  114. {
  115. dataReader = mailclass.GetMailCompleteInfoDbreader(MailID);
  116. }
  117. catch
  118. {
  119. Server.Transfer("../../Error.aspx");
  120. }
  121. if(dataReader.Read())
  122. {
  123. string tmpStr  = "<br/>"+dataReader[7].ToString();
  124. tmpStr  = tmpStr.Replace("<br/>","rn>");
  125. this.txtSubject.Text = "Fw::"+dataReader[4].ToString();
  126. this.txtBody.Text    = ",你好!nnnnnnn";
  127. this.txtBody.Text   += "=======下面是转发邮件=======n";
  128. this.txtBody.Text   += "原邮件发件人姓名:"+dataReader[1].ToString()+"n";
  129. this.txtBody.Text   += "原邮件发件人代号:"+dataReader[10].ToString()+"n";
  130. this.txtBody.Text += tmpStr;
  131. }
  132. dataReader.Close();
  133. try
  134. {
  135. dataReader = mailclass.GetMailAttInfoDbreader(MailID);
  136. }
  137. catch
  138. {
  139. Server.Transfer("../../Error.aspx");
  140. }
  141. while(dataReader.Read())
  142. {
  143. UDS.Components.MailAttachFile att = new MailAttachFile();
  144. att.FileAttribute  = 0;
  145. att.FileSize       = Int32.Parse(dataReader[1].ToString());
  146. att.FileName    = dataReader[0].ToString();
  147. att.FileAuthor     = Username;
  148. att.FileCatlog     = "邮件";
  149. att.FileVisualPath = dataReader[2].ToString();
  150. upattlist.Add(att);
  151. }
  152. BindAttList();
  153. dataReader.Close();
  154. mailclass = null;
  155. }
  156. #endregion
  157. #region 发信人设置
  158. /// <summary>
  159. /// 发信人设置
  160. /// </summary>
  161. private void ReceiverSet()
  162. {
  163. SendToRealName       = (Request.QueryString["Name"]==null)?"":Request.QueryString["Name"].ToString()+",";
  164. SendTo  = (Request.QueryString["Username"]==null)?"":Request.QueryString["Username"].ToString()+",";
  165. }
  166. #endregion
  167. public string GetClassName()
  168. {
  169. if(ClassID=="0")
  170. return "请选择项目";
  171. else
  172. return UDS.Components .ProjectClass .GetProjectName(Int32.Parse(ClassID));
  173. }
  174. #region 初始化下拉列表框
  175. /// <summary>
  176. /// 对下拉列表进行初始化
  177. /// </summary>
  178. private void PopulateListView() 
  179. {
  180. Class cls = new Class();
  181. listImportance.Items.Clear();
  182. listImportance.Items.Add(new ListItem("一般","1"));
  183. listImportance.Items.Add(new ListItem("重要","2"));
  184. listImportance.Items.Add(new ListItem("特别重要","3"));
  185. cblistAttribute.Items.Add(new ListItem("普通邮件","1"));
  186. cblistAttribute.Items.Add(new ListItem("邮件归档","2"));
  187. //cblistAttribute.Items.Add(new ListItem("结案报告","3"));
  188. cblistAttribute.Items[0].Selected=true;
  189. cls = null;
  190. }
  191. #endregion
  192. #region 处理邮件附件上传操作,传回附件数组
  193. /// <summary>
  194. /// 处理上传操作,附件以链接形式存放
  195. /// </summary>
  196. /// <param name="MailReceiverStr"> 用户登陆名字符串,用逗号隔开 </param>
  197. private ArrayList AttUpload(string MailReceiverStr)
  198. {
  199. HtmlForm FrmCompose   = (HtmlForm)this.Page.FindControl("Compose");
  200. string[] RecvIdAr     = System.Text.RegularExpressions.Regex.Split(MailReceiverStr ,",");
  201. ArrayList listattfile = new ArrayList();
  202. // 存放附件至发件人目录中,随机生成目录名
  203. Random TempNameInt    = new Random(); 
  204. string NewMailDirName = TempNameInt.Next(100000000).ToString();
  205. string FileName       = "";
  206. try
  207. {
  208. if(!Directory.Exists(Server.MapPath(".")+"\AttachFiles\"+Username))
  209. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\"+Username);
  210. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewMailDirName);
  211. for (int i=0;i<FrmCompose.Controls.Count;i++)
  212. {
  213. if(FrmCompose.Controls [i].GetType().ToString()=="System.Web.UI.HtmlControls.HtmlInputFile")
  214. {
  215. HtmlInputFile hif = ((HtmlInputFile)(FrmCompose.Controls[i]));
  216. if(hif.PostedFile.FileName.Trim()!="")
  217. {
  218. FileName = System.IO .Path.GetFileName(hif.PostedFile.FileName);
  219. UDS.Components.MailAttachFile att = new MailAttachFile();
  220. // 初始化
  221. att.FileAttribute  = 0;
  222. att.FileSize       = hif.PostedFile.ContentLength;
  223. att.FileName    = FileName;
  224. att.FileAuthor     = Username;
  225. att.FileCatlog     = "邮件";
  226. att.FileVisualPath = "\AttachFiles\"+Username+"\"+NewMailDirName+"\"+FileName;
  227. hif.PostedFile.SaveAs(Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewMailDirName+"\"+FileName);
  228. listattfile.Add(att);
  229. }
  230. hif=null;
  231. }
  232. }
  233. }
  234. catch(Exception ioex)
  235. {
  236. UDS.Components.Error.Log(ioex.ToString());
  237. Server.Transfer("../../Error.aspx");
  238. }
  239. return listattfile;
  240.             
  241. }
  242. #endregion
  243. #region 处理除收件人外的表单传递参数 传回MailMainBody类
  244. /// <summary>
  245. /// 发送不包括附件的邮件正文
  246. /// </summary>
  247. private MailMainBody ProcessFormPost()
  248. {
  249. if(this.cblistAttribute.SelectedIndex.ToString()=="-1")
  250. {
  251. Response.Write("<script language=javascript>alert('请选择邮件类型!');history.go(-1);</script>");
  252. Response.End();
  253. }
  254. if(cblistAttribute.Items[1].Selected)
  255. {
  256. if(Request.Form["hdnProjectID"].ToString()=="0")
  257. {
  258. Response.Write("<script language=javascript>alert('请选择归档项目!');history.go(-1);</script>");
  259. Response.End();
  260. }
  261. }
  262. if(cblistAttribute.Items[0].Selected)
  263. {
  264. // 非空验证
  265. if(Request.Form["hdnTxtSendTo"].ToString()=="")
  266. {
  267. Response.Write("<script language=javascript>alert('请选择收件人!');history.go(-1);</script>");
  268. Response.End();
  269. }
  270. }
  271. // 处理表单传递参数
  272. MailMainBody mailbody = new MailMainBody();
  273. // string cID = "0";
  274. mailbody.MailFolderType = 1; //放入收件夹
  275. mailbody.MailReceiverStr = Request.Form["hdnTxtSendTo"].ToString();
  276. mailbody.MailSendDate = DateTime.Now.ToString();
  277. mailbody.MailSendLevel = 1;//default 1 预留
  278. mailbody.MailSender = Username;
  279. mailbody.MailReceiver = ""; 
  280. mailbody.MailSubject = (txtSubject.Text=="")?"无主题":txtSubject.Text;
  281. mailbody.MailBody = txtBody.Text.Replace("rn","<br/>");
  282. mailbody.MailCcToAddr = Request.Form["hdnTxtCcTo"].ToString();
  283. mailbody.MailBccToAddr = Request.Form["hdnTxtBccTo"].ToString();
  284. mailbody.MailReadFlag = 0;
  285. mailbody.MailTypeFlag = 1;
  286. mailbody.MailClassID        = Request.Form["hdnProjectID"].ToString()!=""?Int32.Parse(Request.Form["hdnProjectID"].ToString()):0;
  287. mailbody.MailImportance = Int32.Parse(listImportance.SelectedItem.Value);
  288. //处理短信提醒
  289. if(this.cbRemind.Checked ==true)
  290. {
  291. SMS sm = new SMS();
  292. sm.SendMsg(Username,mailbody.MailReceiverStr+mailbody.MailCcToAddr+mailbody.MailBccToAddr,"您从"+Username+"处收到了一封新的邮件",1,DateTime.Now,"",0,0);
  293. sm = null;
  294. }
  295. return mailbody;
  296. }
  297. #endregion
  298. #region 文档处理
  299. /// <summary>
  300. /// 文档处理处理,包括文档处理及附件处理
  301. /// </summary>
  302. private void DocProcess(MailMainBody mailbody,ArrayList upattlist)
  303. {
  304. string DocID = "";
  305. int cstRightToApproveDocument = 2;
  306. ProjectClass pjt = new ProjectClass();
  307. DocBody docbody = new DocBody();
  308. docbody.DocTitle = mailbody.MailSubject;
  309. docbody.DocContent = mailbody.MailBody;
  310. docbody.DocAddedBy = mailbody.MailSender;
  311. docbody.DocClassID = mailbody.MailClassID;
  312. docbody.DocAddedDate        = DateTime.Now.ToString();
  313. docbody.DocApprover = (pjt.GetAccessPermission(Int32.Parse(ClassID),Username,cstRightToApproveDocument))?Username:"";
  314. docbody.DocApproveDate = (pjt.GetAccessPermission(Int32.Parse(ClassID),Username,cstRightToApproveDocument))?DateTime.Now.ToString():"";
  315. docbody.DocApproved         = (docbody.DocApprover =="")?0:1;
  316. docbody.DocAttribute        = 0;
  317. docbody.DocType = 0;
  318. DocumentClass doc = new DocumentClass();
  319. // 添加文档,并返回ID
  320. DocID = doc.AddDocBody(docbody);
  321. foreach(UDS.Components.MailAttachFile  att in upattlist)
  322. {
  323. try
  324. {
  325. DocAttachFile docatt = new DocAttachFile();
  326. //  Mail Attach File 类转换为Doc Attach File
  327. docatt.FileAttribute  = 0;
  328. docatt.FileSize       = att.FileSize;
  329. docatt.FileName       = att.FileName;
  330. docatt.FileAuthor     = Username;
  331. docatt.FileCatlog     = "文档";
  332. docatt.FileVisualPath = "Mail"+att.FileVisualPath;
  333. docatt.FileAddedDate  = DateTime.Now.ToString();
  334. docatt.DocID          = Int32.Parse(DocID);
  335. // 转换结束
  336. // 附件数据库处理
  337. doc.AddAttach(docatt,Int32.Parse(DocID));
  338. }
  339. catch(Exception ioex)
  340. {
  341. UDS.Components.Error.Log(ioex.ToString());
  342. Server.Transfer("../../Error.aspx");
  343. }
  344. }
  345. }
  346. #endregion
  347. /// <summary>
  348. /// 发信处理,包括邮件处理及附件处理
  349. /// </summary>
  350. private void btnSendMail_Click(object sender, System.EventArgs e)
  351. {
  352. HttpCookie UserCookie = Request.Cookies["Username"];
  353. Username = UserCookie.Value.ToString();
  354. MailClass mailclass   = new UDS.Components.MailClass();
  355. MailMainBody mailbody = new UDS.Components.MailMainBody(); 
  356. ArrayList listmail    = new ArrayList();
  357. ArrayList upattlist = (ArrayList)Session["upattlist"];
  358. // ArrayList listattfile = new ArrayList();
  359. // 得到mailbody类
  360. mailbody = ProcessFormPost();
  361. //邮件处理
  362. try
  363. {
  364. listmail     = mailclass.MailSend(mailbody); // 返回已经发送的邮件ID列表(包括抄送和密抄)
  365. // listattfile  = AttUpload(mailbody.MailReceiverStr.ToString());   // 返回邮件附件数组
  366. foreach(string mailID in listmail)
  367. {   // 循环发送邮件附件
  368. foreach(UDS.Components.MailAttachFile att in upattlist)
  369. {   // 处理邮件附件的数据库操作部分 
  370. Random TempNameInt    = new Random(); 
  371. string NewDocDirName  = TempNameInt.Next(100000000).ToString();
  372. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewDocDirName);
  373. //File.Move(Server.MapPath(".")+"\"+att.FileVisualPath ,Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewDocDirName+"\"+att.FileName);
  374. File.Copy(Server.MapPath(".")+"\"+att.FileVisualPath ,Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewDocDirName+"\"+att.FileName,true);
  375. // Directory.Delete(Server.MapPath(".")+"\"+att.FileVisualPath.ToString().Replace(att.FileName,""),true);
  376. att.FileVisualPath    = "\AttachFiles\"+Username+"\"+NewDocDirName+"\"+att.FileName;
  377. mailclass.AttSend(att,Int32.Parse(mailID));
  378. }
  379. }
  380. if(cblistAttribute.Items[1].Selected&&Request.Form["hdnProjectID"].ToString()!="0")
  381. {
  382. DocProcess(mailbody,upattlist);
  383. }
  384. Response.Write("<script language=javascript>alert('邮件发送成功!');if(parent.frames.length==0) window.close();else self.location=('Index.aspx');</script>");
  385. }
  386. catch (Exception sendex)
  387. {
  388. UDS.Components.Error.Log(sendex.ToString());
  389. Server.Transfer("../../Error.aspx");
  390. }
  391. }
  392. #region Web Form Designer generated code
  393. override protected void OnInit(EventArgs e)
  394. {
  395. //
  396. // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
  397. //
  398. InitializeComponent();
  399. base.OnInit(e);
  400. }
  401. /// <summary>
  402. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  403. /// 此方法的内容。
  404. /// </summary>
  405. private void InitializeComponent()
  406. {    
  407. this.txtSubject.TextChanged += new System.EventHandler(this.txtSubject_TextChanged);
  408. this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
  409. this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
  410. this.btnSendMail.Click += new System.EventHandler(this.btnSendMail_Click);
  411. this.Load += new System.EventHandler(this.Page_Load);
  412. }
  413. #endregion
  414. private void txtSubject_TextChanged(object sender, System.EventArgs e)
  415. {
  416. }
  417. private void BindAttList()
  418. {
  419. this.listUp.Items.Clear();
  420. int count = 0;
  421. ArrayList upattlist = (ArrayList)Session["upattlist"];
  422. foreach(UDS.Components.MailAttachFile  att in upattlist)
  423. {
  424. count++;
  425. this.listUp.Items.Add(new ListItem(att.FileName.ToString(),count.ToString()));
  426. }
  427. }
  428. private void btnUpload_Click(object sender, System.EventArgs e)
  429. {
  430. HtmlForm FrmCompose   = (HtmlForm)this.Page.FindControl("Compose");
  431. Random TempNameInt    = new Random(); 
  432. string NewMailDirName  = TempNameInt.Next(100000000).ToString();
  433. // 存放附件至提交人目录中,随机生成目录名
  434. ArrayList upattlist = (ArrayList)Session["upattlist"];
  435. string FileName       = "";
  436. try
  437. if(!Directory.Exists(Server.MapPath(".")+"\AttachFiles\tmp"))
  438. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\tmp");
  439. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\tmp\"+NewMailDirName+Username);
  440. for (int i=0;i<FrmCompose.Controls.Count;i++)
  441. {
  442. if(FrmCompose.Controls [i].GetType().ToString()=="System.Web.UI.HtmlControls.HtmlInputFile")
  443. {
  444. HtmlInputFile hif = ((HtmlInputFile)(FrmCompose.Controls[i]));
  445. if(hif.PostedFile.FileName.Trim()!="")
  446. {
  447. FileName = System.IO .Path.GetFileName(hif.PostedFile.FileName);
  448. UDS.Components.MailAttachFile att = new MailAttachFile();
  449. // 初始化
  450. att.FileAttribute  = 0;
  451. att.FileSize       = hif.PostedFile.ContentLength;
  452. att.FileName    = FileName;
  453. att.FileAuthor     = Username;
  454. att.FileCatlog     = "邮件";
  455. att.FileVisualPath = "\AttachFiles\tmp\"+NewMailDirName+Username+"\"+FileName;
  456. hif.PostedFile.SaveAs(Server.MapPath(".")+"\AttachFiles\tmp\"+NewMailDirName+Username+"\"+FileName);
  457. upattlist.Add(att);
  458. }
  459. hif=null;
  460. }
  461. }
  462. Session["upattlist"] = upattlist;
  463. BindAttList();
  464. this.SendToRealName       = Request.Form["txtSendTo"].ToString();
  465. this.SendTo   = Request.Form["hdnTxtSendTo"].ToString();
  466. this.CcTo   = Request.Form["hdnTxtCcTo"].ToString();
  467. this.BccTo   = Request.Form["hdnTxtBccTo"].ToString();
  468. this.CcToRealName         = Request.Form["txtCcTo"].ToString();
  469. this.BccToRealName        = Request.Form["txtBccTo"].ToString();  
  470. catch(Exception ioex)
  471. {
  472. UDS.Components.Error.Log(ioex.ToString());
  473. Server.Transfer("../../Error.aspx");
  474. }
  475.             
  476. }
  477. private void btnRemove_Click(object sender, System.EventArgs e)
  478. {
  479. ArrayList upattlist = (ArrayList)Session["upattlist"];
  480. for(int i=listUp.Items.Count-1;i>=0;i--)
  481. {
  482. if(this.listUp.Items[i].Selected)
  483. {
  484. this.listUp.Items.RemoveAt(i);
  485. upattlist.RemoveAt (i);
  486. }
  487. }
  488. Session["upattlist"] = upattlist;
  489. this.SendToRealName       = Request.Form["txtSendTo"].ToString();
  490. this.SendTo   = Request.Form["hdnTxtSendTo"].ToString();
  491. this.CcTo   = Request.Form["hdnTxtCcTo"].ToString();
  492. this.BccTo   = Request.Form["hdnTxtBccTo"].ToString();
  493. this.CcToRealName         = Request.Form["txtCcTo"].ToString();
  494. this.BccToRealName        = Request.Form["txtBccTo"].ToString();  
  495. }
  496. }
  497. }