mail_list_receive.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:30k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. public partial class web_mail_mail_list_receive : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../", 2);
  17.         if (!IsPostBack)
  18.         {
  19.             //設置主鍵
  20.             MailList.DataKeyNames = new string[] { "id" };
  21.             if (Request.QueryString["r_id"] != null)
  22.             {
  23.                 PnlMail.Visible = true;
  24.                 this.readMail(Request.QueryString["r_id"]);
  25.                 this.setMail(Request.QueryString["r_id"]);
  26.             }
  27.         }
  28.     }
  29.     protected void readMail(string id)
  30.     {
  31.         DateMgr mgr = new DateMgr();
  32.         string time = mgr.getDateTime();
  33.         SqlConnection conn = dbConnection.getConnection();
  34.         conn.Open();
  35.         SqlCommand cmd = new SqlCommand("update OA_MAIL_RECEIVER set is_read=1, read_date='"+
  36.             time+"' where id="+id, conn);
  37.         cmd.ExecuteNonQuery();
  38.         conn.Close();
  39.         MailList.DataBind();
  40.     }
  41.     protected void setMail(string r_id)
  42.     {
  43.         try
  44.         {
  45.             SqlConnection conn = dbConnection.getConnection();
  46.             conn.Open();
  47.             SqlCommand cmd = new SqlCommand("SELECT r.mail_id, m.title, m.content, s.send_date, "+
  48.                 "e.id, e.emp_no, e.emp_name, p.name position FROM dbo.OA_MAIL_RECEIVER AS r INNER JOIN "+
  49.                 "dbo.OA_MAIL_SENDER AS s ON r.mail_id = s.mail_id INNER JOIN dbo.OA_EMPLOYEE AS e "+
  50.                 "ON s.sender = e.id INNER JOIN dbo.OA_MAIL_CONTENT AS m ON r.mail_id = m.id inner "+
  51.                 "join OA_EMPLOYEE_POSITION p on e.position=p.id WHERE r.id="+r_id, conn);
  52.             SqlDataReader dr = cmd.ExecuteReader();
  53.             string mail_id = "0";
  54.             if (dr.Read())
  55.             {
  56.                 mail_id = dr["mail_id"].ToString();
  57.                 LblTitle.Text = dr["title"].ToString();
  58.                 TxtContent.Text = dr["content"].ToString().Replace("<br>", "n").Replace("&nbsp;"," ");
  59.                 LblSendDate.Text = dr["send_date"].ToString();
  60.                 SenderId.Value = dr["id"].ToString();
  61.                 LblSender.Text = dr["emp_no"].ToString() + " " + dr["emp_name"].ToString() + dr["position"].ToString();
  62.             }
  63.             dr.Close();
  64.             cmd = new SqlCommand("select e.id, e.emp_no, e.emp_name, p.name from OA_MAIL_RECEIVER r inner join " +
  65.                 "OA_EMPLOYEE e on r.receiver=e.id inner join OA_EMPLOYEE_POSITION p on e.position=p.id "+
  66.                 "where mail_id="+mail_id, conn);
  67.             dr = cmd.ExecuteReader();
  68.             while (dr.Read())
  69.             {
  70.                 ReceiverIds.Value = ReceiverIds.Value + dr["id"].ToString() + ";";
  71.                 LblReceivers.Text = LblReceivers.Text + dr["emp_no"].ToString() + " " + 
  72.                     dr["emp_name"].ToString() + dr["name"].ToString() + "; ";
  73.             }
  74.             dr.Close();
  75.             if(!LblReceivers.Text.Equals(""))
  76.             {
  77.                 LblReceivers.Text = LblReceivers.Text.Substring(0, LblReceivers.Text.Length - 2);
  78.             }
  79.             cmd = new SqlCommand("select cn_name, en_name, content_length from OA_MAIL_ATTACHMENT where mail_id=" + mail_id, conn);
  80.             dr = cmd.ExecuteReader();
  81.             if (dr.Read())
  82.             {
  83.                 BtnAttachment.Text = dr["cn_name"].ToString();
  84.                 AttachmentEnName.Value = dr["en_name"].ToString();
  85.                 ContentLength.Value = dr["content_length"].ToString();
  86.             }
  87.             else
  88.             {
  89.                 //沒有附件
  90.                 LblNoAttachment.Visible = true;
  91.                 BtnAttachment.Visible = false;
  92.             }
  93.             dr.Close();
  94.             conn.Close();
  95.         }
  96.         catch (Exception ex)
  97.         {
  98.             Response.Write(ex.ToString());
  99.         }
  100.     }
  101.     protected void MailList_RowDataBound(object sender, GridViewRowEventArgs e)
  102.     {
  103.         //行高亮
  104.         if (e.Row.RowType == DataControlRowType.DataRow)
  105.         {
  106.             e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer';");
  107.             e.Row.Attributes.Add("onclick", "location.href='mail_list_receive.aspx?r_id=" + MailList.DataKeys[e.Row.RowIndex].Value.ToString() + "'");
  108.         }
  109.     }
  110.     protected void MailList_RowCreated(object sender, GridViewRowEventArgs e)
  111.     {
  112.         if (e.Row.RowType == DataControlRowType.DataRow)
  113.         {
  114.             LinkButton btnDelete = (LinkButton)e.Row.FindControl("BtnDelete");
  115.             btnDelete.CommandArgument = MailList.DataKeys[e.Row.RowIndex].Value.ToString();
  116.             SqlConnection conn = dbConnection.getConnection();
  117.             conn.Open();
  118.             SqlCommand cmd = new SqlCommand("select is_read from OA_MAIL_RECEIVER where id=" + MailList.DataKeys[e.Row.RowIndex].Value.ToString(), conn);
  119.             int is_read = Convert.ToInt32(cmd.ExecuteScalar());
  120.             Label label = (Label)e.Row.FindControl("LblState");
  121.             if (is_read == 1)
  122.             {
  123.                 label.Text = "已讀";
  124.                 label.ForeColor = System.Drawing.Color.Blue;
  125.             }
  126.             else
  127.             {
  128.                 label.Text = "未讀";
  129.                 label.ForeColor = System.Drawing.Color.Red;
  130.             }
  131.             conn.Close();
  132.         }
  133.     }
  134.     protected void MailList_RowCommand(object sender, GridViewCommandEventArgs e)
  135.     {
  136.         if (e.CommandName.Equals("DeleteData"))
  137.         {
  138.             string id = e.CommandArgument.ToString();
  139.             //點擊刪除按鈕
  140.             try
  141.             {
  142.                 SqlConnection conn = dbConnection.getConnection();
  143.                 conn.Open();
  144.                 SqlCommand cmd = new SqlCommand("update OA_MAIL_RECEIVER set is_del=1 where id=" + id, conn);
  145.                 cmd.ExecuteNonQuery(); 
  146.                 MailList.DataBind();
  147.             }
  148.             catch (Exception ex)
  149.             {
  150.                 Response.Write(ex.Message);
  151.             }
  152.         }
  153.     }
  154.     protected void BtnAttachment_Click(object sender, EventArgs e)
  155.     {
  156.         this.DownLoadFile(AttachmentEnName.Value);
  157.     }
  158.     protected void DownLoadFile(string name)
  159.     {
  160.         string directory = "\web\oa_upload\mail\";
  161.         try
  162.         {
  163.             Response.Clear();
  164.             Response.ClearHeaders();
  165.             Response.Buffer = false;
  166.             Response.ContentType = "application/octet-stream";
  167.             Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(BtnAttachment.Text, System.Text.Encoding.UTF8));
  168.             Response.AppendHeader("Content-Length", ContentLength.Value);
  169.             Response.WriteFile(directory + name);
  170.             Response.Flush();
  171.             Response.End();
  172.         }
  173.         catch (Exception ex)
  174.         {
  175.             Response.Write(ex.Message);
  176.         }
  177.     }
  178.     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  179.     {
  180.         SqlConnection conn = dbConnection.getConnection();
  181.         conn.Open();
  182.         SqlCommand cmd = new SqlCommand("update OA_MAIL_RECEIVER set is_del=1 where id=" + Request.QueryString["r_id"], conn);
  183.         cmd.ExecuteNonQuery();
  184.         conn.Close();
  185.         MailList.DataBind();
  186.         PnlMail.Visible = false;
  187.     }
  188.     protected void BtnReply_Click(object sender, EventArgs e)
  189.     {
  190.         TxtReplyReceiver.Text = LblSender.Text;
  191.         TxtReplyTitle.Text = "RE:" + LblTitle.Text;
  192.         TxtReplyContent.Text = "nnn--------------------------------------------------------n";
  193.         TxtReplyContent.Text = TxtReplyContent.Text + "From:" + LblSender.Text + "n";
  194.         TxtReplyContent.Text = TxtReplyContent.Text + "Sent:" + LblSendDate.Text + "n";
  195.         TxtReplyContent.Text = TxtReplyContent.Text + "To:" + LblReceivers.Text + "n";
  196.         TxtReplyContent.Text = TxtReplyContent.Text + "Subject:" + LblTitle.Text + "nn";
  197.         TxtReplyContent.Text = TxtReplyContent.Text + TxtContent.Text+"nn";
  198.         if (!BtnAttachment.Text.Equals(""))
  199.         {
  200.             TxtReplyContent.Text = TxtReplyContent.Text + "Attachment:" + BtnAttachment.Text + "n";
  201.         }
  202.         PnlReply.Visible = true;
  203.         PnlReplyAll.Visible = false;
  204.         PnlMail.Visible = false;
  205.         PnlFw.Visible = false;
  206.         TxtReplyContent.Focus();
  207.     }
  208.     protected void BtnReplyOk_Click(object sender, EventArgs e)
  209.     {
  210.         int flag = 0;
  211.         if (TxtReplyTitle.Text == "")
  212.         {
  213.             flag = 1;
  214.             Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
  215.         }
  216.         if (TxtReplyContent.Text == "")
  217.         {
  218.             flag = 1;
  219.             Response.Write("<script language='javascript'>alert('郵件正文不能為空!');</script>");
  220.         }
  221.         if (flag == 0)
  222.         {
  223.             DateMgr date = new DateMgr();
  224.             string time = date.getTime();
  225.             //上傳檔案
  226.             if (!this.uploadReplyFile(time))
  227.             {
  228.                 if (FileUpload1.HasFile)
  229.                 {
  230.                     //上傳檔案不成功
  231.                 }
  232.                 else
  233.                 {
  234.                     //沒有上傳附件
  235.                     this.saveMail();
  236.                 }
  237.             }
  238.             else
  239.             {
  240.                 this.saveMail();
  241.             }
  242.         }
  243.     }
  244.     protected void saveMail()
  245.     {
  246.         #region
  247.         //添加數据
  248.         SqlConnection conn = dbConnection.getConnection();
  249.         conn.Open();
  250.         int mail_id = 1;
  251.         SqlCommand cmd = new SqlCommand("select max(id) from OA_MAIL_CONTENT", conn);
  252.         try
  253.         {
  254.             mail_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  255.         }
  256.         catch { }
  257.         int sender_id = 1;
  258.         cmd = new SqlCommand("select max(id) from OA_MAIL_SENDER", conn);
  259.         try
  260.         {
  261.             sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  262.         }
  263.         catch { }
  264.         int receiver_id = 1;
  265.         cmd = new SqlCommand("select max(id) from OA_MAIL_RECEIVER", conn);
  266.         try
  267.         {
  268.             receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  269.         }
  270.         catch { }
  271.         int attachment_id = 1;
  272.         cmd = new SqlCommand("select max(id) from OA_MAIL_ATTACHMENT", conn);
  273.         try
  274.         {
  275.             attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  276.         }
  277.         catch { }
  278.         #endregion
  279.         SqlTransaction tx = conn.BeginTransaction();
  280.         try
  281.         {
  282.             //添加郵件內容
  283.             cmd = new SqlCommand("insert into OA_MAIL_CONTENT(id, title, content) values (" +
  284.                 mail_id + ", '" + TxtReplyTitle.Text + "', '" + TxtReplyContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "')", conn);
  285.             cmd.Transaction = tx;
  286.             cmd.ExecuteNonQuery();
  287.             //添加發件人信息
  288.             int user_id = 0;
  289.             if (Session["user"] != null)
  290.             {
  291.                 user_id = ((User)Session["user"]).Id;
  292.             }
  293.             DateMgr mgr = new DateMgr();
  294.             string time = mgr.getDateTime();
  295.             cmd = new SqlCommand("insert into OA_MAIL_SENDER(id, mail_id, sender, state, " +
  296.                 "is_del, send_date, cre_date)values(" + sender_id + ", " + mail_id + ", " + user_id +
  297.                 ", 1, 0, '" + time + "', '" + time + "')", conn);
  298.             cmd.Transaction = tx;
  299.             cmd.ExecuteNonQuery();
  300.             //添加收件人
  301.             cmd = new SqlCommand("insert into OA_MAIL_RECEIVER(id, mail_id, " +
  302.                 "receiver, is_read, is_del, read_date) values (" + receiver_id + ", " +
  303.                 mail_id + ", " + SenderId.Value + ", 0, 0, '')", conn);
  304.             cmd.Transaction = tx;
  305.             cmd.ExecuteNonQuery();
  306.             //添加附件
  307.             if (!FileUpload1.FileName.Equals(""))
  308.             {
  309.                 DateMgr date = new DateMgr();
  310.                 time = date.getTime();
  311.                 string name = "";
  312.                 try
  313.                 {
  314.                     name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  315.                 }
  316.                 catch { }
  317.                 cmd = new SqlCommand("insert into OA_MAIL_ATTACHMENT(id, mail_id, cn_name, en_name, content_length)" +
  318.                     " values (" + attachment_id + ", " + mail_id + ", '"+FileUpload1.FileName+"','" + name + "','" +
  319.                     ContentLength1.Value + "')", conn);
  320.                 cmd.Transaction = tx;
  321.                 cmd.ExecuteNonQuery();
  322.             }
  323.             tx.Commit();
  324.             Response.Redirect("mail_list_send.aspx");
  325.         }
  326.         catch (Exception ex)
  327.         {
  328.             Response.Write(ex.Message);
  329.             tx.Rollback();
  330.         }
  331.         conn.Close();
  332.     }
  333.     protected bool uploadReplyFile(string time)
  334.     {
  335.         bool flag = false;
  336.         string server_ip = "";
  337.         string root = "";
  338.         string folder = "";
  339.         if (Application["FILE_SERVER_IP"] != null)
  340.         {
  341.             server_ip = Application["FILE_SERVER_IP"].ToString();
  342.         }
  343.         if (Application["FILE_SERVER_ROOT"] != null)
  344.         {
  345.             root = Application["FILE_SERVER_ROOT"].ToString();
  346.         }
  347.         if (Application["MAIL_FOLDER"] != null)
  348.         {
  349.             folder = Application["MAIL_FOLDER"].ToString();
  350.         }
  351.         //string directory = "\\" + server_ip + "\" + root + "\" + folder+"\";
  352.         string directory = "f:\oa(new)\web\oa_upload\mail\";
  353.         if (FileUpload1.HasFile)
  354.         {
  355.             bool extenstion = false;
  356.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  357.             string[] allowedExtensions = 
  358.                 { ".gif", ".jpg", ".doc", ".xls", ".rar",".pdf", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  359.             for (int i = 0; i < allowedExtensions.Length; i++)
  360.             {
  361.                 if (fileExtension == allowedExtensions[i])
  362.                 {
  363.                     extenstion = true;
  364.                     break;
  365.                 }
  366.             }
  367.             if (!extenstion)
  368.             {
  369.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif,pdf, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  370.                 return false;
  371.             }
  372.             else
  373.             {
  374.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  375.                 FileUpload1.PostedFile.SaveAs(directory + name);
  376.                 ContentLength1.Value = FileUpload1.PostedFile.ContentLength.ToString();
  377.                 flag = true;
  378.             }
  379.         }
  380.         return flag;
  381.     }
  382.     protected void BtnReplyAllOk_Click(object sender, EventArgs e)
  383.     {
  384.         int flag = 0;
  385.         if (TxtReplyAllTitle.Text == "")
  386.         {
  387.             flag = 1;
  388.             Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
  389.         }
  390.         if (TxtReplyAllContent.Text == "")
  391.         {
  392.             flag = 1;
  393.             Response.Write("<script language='javascript'>alert('郵件正文不能為空!');</script>");
  394.         }
  395.         if (flag == 0)
  396.         {
  397.             DateMgr date = new DateMgr();
  398.             string time = date.getTime();
  399.             //上傳檔案
  400.             if (!this.uploadReplyAllFile(time))
  401.             {
  402.                 if (FileUpload2.HasFile)
  403.                 {
  404.                     //上傳檔案不成功
  405.                 }
  406.                 else
  407.                 {
  408.                     //沒有上傳附件
  409.                     this.saveReplyAllMail();
  410.                 }
  411.             }
  412.             else
  413.             {
  414.                 this.saveReplyAllMail();
  415.             }
  416.         }
  417.     }
  418.     protected bool uploadReplyAllFile(string time)
  419.     {
  420.         bool flag = false;
  421.         string server_ip = "";
  422.         string root = "";
  423.         string folder = "";
  424.         if (Application["FILE_SERVER_IP"] != null)
  425.         {
  426.             server_ip = Application["FILE_SERVER_IP"].ToString();
  427.         }
  428.         if (Application["FILE_SERVER_ROOT"] != null)
  429.         {
  430.             root = Application["FILE_SERVER_ROOT"].ToString();
  431.         }
  432.         if (Application["MAIL_FOLDER"] != null)
  433.         {
  434.             folder = Application["MAIL_FOLDER"].ToString();
  435.         }
  436.         //string directory = "\\" + server_ip + "\" + root + "\" + folder+"\";
  437.         string directory = "f:\oa(new)\web\oa_upload\mail\";
  438.         if (FileUpload2.HasFile)
  439.         {
  440.             bool extenstion = false;
  441.             string fileExtension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
  442.             string[] allowedExtensions = 
  443.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip",".pdf", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  444.             for (int i = 0; i < allowedExtensions.Length; i++)
  445.             {
  446.                 if (fileExtension == allowedExtensions[i])
  447.                 {
  448.                     extenstion = true;
  449.                     break;
  450.                 }
  451.             }
  452.             if (!extenstion)
  453.             {
  454.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, pdf,jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  455.                 return false;
  456.             }
  457.             else
  458.             {
  459.                 string name = FileUpload2.FileName.Substring(0, FileUpload2.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
  460.                 FileUpload2.PostedFile.SaveAs(directory + name);
  461.                 ContentLength2.Value = FileUpload2.PostedFile.ContentLength.ToString();
  462.                 flag = true;
  463.             }
  464.         }
  465.         return flag;
  466.     }
  467.     protected void saveReplyAllMail()
  468.     {
  469.         //添加數据
  470.         SqlConnection conn = dbConnection.getConnection();
  471.         conn.Open();
  472.         int mail_id = 1;
  473.         SqlCommand cmd = new SqlCommand("select max(id) from OA_MAIL_CONTENT", conn);
  474.         try
  475.         {
  476.             mail_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  477.         }
  478.         catch { }
  479.         int sender_id = 1;
  480.         cmd = new SqlCommand("select max(id) from OA_MAIL_SENDER", conn);
  481.         try
  482.         {
  483.             sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  484.         }
  485.         catch { }
  486.         int receiver_id = 1;
  487.         cmd = new SqlCommand("select max(id) from OA_MAIL_RECEIVER", conn);
  488.         try
  489.         {
  490.             receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  491.         }
  492.         catch { }
  493.         int attachment_id = 1;
  494.         cmd = new SqlCommand("select max(id) from OA_MAIL_ATTACHMENT", conn);
  495.         try
  496.         {
  497.             attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  498.         }
  499.         catch { }
  500.         SqlTransaction tx = conn.BeginTransaction();
  501.         try
  502.         {
  503.             //添加郵件內容
  504.             cmd = new SqlCommand("insert into OA_MAIL_CONTENT(id, title, content) values (" +
  505.                 mail_id + ", '" + TxtReplyAllTitle.Text + "', '" + TxtReplyAllContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "')", conn);
  506.             cmd.Transaction = tx;
  507.             cmd.ExecuteNonQuery();
  508.             //添加發件人信息
  509.             int user_id = 0;
  510.             if (Session["user"] != null)
  511.             {
  512.                 user_id = ((User)Session["user"]).Id;
  513.             }
  514.             DateMgr mgr = new DateMgr();
  515.             string time = mgr.getDateTime();
  516.             cmd = new SqlCommand("insert into OA_MAIL_SENDER(id, mail_id, sender, state, " +
  517.                 "is_del, send_date, cre_date)values(" + sender_id + ", " + mail_id + ", " + user_id +
  518.                 ", 1, 0, '" + time + "', '" + time + "')", conn);
  519.             cmd.Transaction = tx;
  520.             cmd.ExecuteNonQuery();
  521.             //添加收件人
  522.             string[] receiver_ids = ReceiverIds.Value.Split(new char[] { ';' });
  523.             for (int i = 0; i < receiver_ids.Length-1; i++)
  524.             {
  525.                 cmd = new SqlCommand("insert into OA_MAIL_RECEIVER(id, mail_id, " +
  526.                     "receiver, is_read, is_del, read_date) values (" + receiver_id + ", " +
  527.                     mail_id + ", " + receiver_ids[i] + ", 0, 0, '')", conn);
  528.                 cmd.Transaction = tx;
  529.                 cmd.ExecuteNonQuery();
  530.                 receiver_id++;
  531.             }
  532.             //添加附件
  533.             if (!FileUpload2.FileName.Equals(""))
  534.             {
  535.                 DateMgr date = new DateMgr();
  536.                 time = date.getTime();
  537.                 string name = "";
  538.                 try
  539.                 {
  540.                     name = FileUpload2.FileName.Substring(0, FileUpload2.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
  541.                 }
  542.                 catch { }
  543.                 cmd = new SqlCommand("insert into OA_MAIL_ATTACHMENT(id, mail_id, cn_name,en_name, content_length)" +
  544.                     " values (" + attachment_id + ", " + mail_id + ", '"+FileUpload2.FileName+"', '" + name + "','" +
  545.                     ContentLength2.Value + "')", conn);
  546.                 cmd.Transaction = tx;
  547.                 cmd.ExecuteNonQuery();
  548.             }
  549.             tx.Commit();
  550.             Response.Redirect("mail_list_send.aspx");
  551.         }
  552.         catch (Exception ex)
  553.         {
  554.             Response.Write(ex.Message);
  555.             tx.Rollback();
  556.         }
  557.         conn.Close();
  558.     }
  559.     protected void BtnReplyAll_Click(object sender, EventArgs e)
  560.     {
  561.         if (!ReceiverIds.Value.Contains(SenderId.Value))
  562.         {
  563.             TxtReplyAllReceiver.Text = LblSender.Text + ";" + LblReceivers.Text;
  564.             ReceiverIds.Value = SenderId.Value + ";" + ReceiverIds.Value;
  565.         }
  566.         else
  567.         {
  568.             TxtReplyAllReceiver.Text = LblReceivers.Text;
  569.         }
  570.         
  571.         TxtReplyAllTitle.Text = "RE:" + LblTitle.Text;
  572.         TxtReplyAllContent.Text = "nnn--------------------------------------------------------n";
  573.         TxtReplyAllContent.Text = TxtReplyAllContent.Text + "From:" + LblSender.Text + "n";
  574.         TxtReplyAllContent.Text = TxtReplyAllContent.Text + "Sent:" + LblSendDate.Text + "n";
  575.         TxtReplyAllContent.Text = TxtReplyAllContent.Text + "To:" + LblReceivers.Text + "n";
  576.         TxtReplyAllContent.Text = TxtReplyAllContent.Text + "Subject:" + LblTitle.Text + "nn";
  577.         TxtReplyAllContent.Text = TxtReplyAllContent.Text + TxtContent.Text + "nn";
  578.         if (!BtnAttachment.Text.Equals(""))
  579.         {
  580.             TxtReplyAllContent.Text = TxtReplyAllContent.Text + "Attachment:" + BtnAttachment.Text + "n";
  581.         }
  582.         PnlReply.Visible = false;
  583.         PnlReplyAll.Visible = true;
  584.         PnlMail.Visible = false;
  585.         PnlFw.Visible = false;
  586.         TxtReplyAllContent.Focus();
  587.     }
  588.     protected void BtnFw_Click(object sender, EventArgs e)
  589.     {
  590.         if (!BtnAttachment.Text.Equals(""))
  591.         {
  592.             TxtFile.Text = BtnAttachment.Text;
  593.         }
  594.         else
  595.         {
  596.             TxtFile.Text = "無";
  597.         }
  598.         TxtFwTitle.Text = "FW:" + LblTitle.Text;
  599.         TxtFwContent.Text = "nnn--------------------------------------------------------n";
  600.         TxtFwContent.Text = TxtFwContent.Text + "From:" + LblSender.Text + "n";
  601.         TxtFwContent.Text = TxtFwContent.Text + "Sent:" + LblSendDate.Text + "n";
  602.         TxtFwContent.Text = TxtFwContent.Text + "To:" + LblReceivers.Text + "n";
  603.         TxtFwContent.Text = TxtFwContent.Text + "Subject:" + LblTitle.Text + "nn";
  604.         TxtFwContent.Text = TxtFwContent.Text + TxtContent.Text + "nn";
  605.         if (!BtnAttachment.Text.Equals(""))
  606.         {
  607.             TxtFwContent.Text = TxtFwContent.Text + "Attachment:" + BtnAttachment.Text + "n";
  608.         }
  609.         PnlReply.Visible = false;
  610.         PnlReplyAll.Visible = false;
  611.         PnlMail.Visible = false;
  612.         PnlFw.Visible = true;
  613.     }
  614.     protected void BtnFwOk_Click(object sender, EventArgs e)
  615.     {
  616.         int flag = 0;
  617.         if (Employee_ids.Value == "")
  618.         {
  619.             flag = 1;
  620.             Response.Write("<script language='javascript'>alert('收件人不能為空!');</script>");
  621.         }
  622.         if (TxtFwTitle.Text == "")
  623.         {
  624.             flag = 1;
  625.             Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
  626.         }
  627.         if (TxtFwContent.Text == "")
  628.         {
  629.             flag = 1;
  630.             Response.Write("<script language='javascript'>alert('郵件正文不能為空!');</script>");
  631.         }
  632.         if (flag == 0)
  633.         {
  634.             this.saveFwMail();         
  635.         }
  636.     }
  637.     protected void saveFwMail()
  638.     {
  639.         //添加數据
  640.         SqlConnection conn = dbConnection.getConnection();
  641.         conn.Open();
  642.         int mail_id = 1;
  643.         SqlCommand cmd = new SqlCommand("select max(id) from OA_MAIL_CONTENT", conn);
  644.         try
  645.         {
  646.             mail_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  647.         }
  648.         catch { }
  649.         int sender_id = 1;
  650.         cmd = new SqlCommand("select max(id) from OA_MAIL_SENDER", conn);
  651.         try
  652.         {
  653.             sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  654.         }
  655.         catch { }
  656.         int receiver_id = 1;
  657.         cmd = new SqlCommand("select max(id) from OA_MAIL_RECEIVER", conn);
  658.         try
  659.         {
  660.             receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  661.         }
  662.         catch { }
  663.         int attachment_id = 1;
  664.         cmd = new SqlCommand("select max(id) from OA_MAIL_ATTACHMENT", conn);
  665.         try
  666.         {
  667.             attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  668.         }
  669.         catch { }
  670.         SqlTransaction tx = conn.BeginTransaction();
  671.         try
  672.         {
  673.             //添加郵件內容
  674.             cmd = new SqlCommand("insert into OA_MAIL_CONTENT(id, title, content) values (" +
  675.                 mail_id + ", '" + TxtFwTitle.Text + "', '" + TxtFwContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "')", conn);
  676.             cmd.Transaction = tx;
  677.             cmd.ExecuteNonQuery();
  678.             //添加發件人信息
  679.             int user_id = 0;
  680.             if (Session["user"] != null)
  681.             {
  682.                 user_id = ((User)Session["user"]).Id;
  683.             }
  684.             DateMgr mgr = new DateMgr();
  685.             string time = mgr.getDateTime();
  686.             cmd = new SqlCommand("insert into OA_MAIL_SENDER(id, mail_id, sender, state, " +
  687.                 "is_del, send_date, cre_date)values(" + sender_id + ", " + mail_id + ", " + user_id +
  688.                 ", 1, 0, '" + time + "', '" + time + "')", conn);
  689.             cmd.Transaction = tx;
  690.             cmd.ExecuteNonQuery();
  691.             //添加收件人
  692.             if (!Employee_ids.Value.Equals(""))
  693.             {
  694.                 string[] receiver_ids = Employee_ids.Value.Split(new char[] { ';' });
  695.                 for (int i = 0; i < receiver_ids.Length; i++)
  696.                 {
  697.                     cmd = new SqlCommand("insert into OA_MAIL_RECEIVER(id, mail_id, " +
  698.                         "receiver, is_read, is_del, read_date) values (" + receiver_id + ", " +
  699.                         mail_id + ", " + receiver_ids[i] + ", 0, 0, '')", conn);
  700.                     cmd.Transaction = tx;
  701.                     cmd.ExecuteNonQuery();
  702.                     receiver_id++;
  703.                 }
  704.             }
  705.             //添加附件
  706.             if (!TxtFile.Text.Equals("無"))
  707.             {
  708.                 if (Request.QueryString["r_id"] != null)
  709.                 {
  710.                     cmd = new SqlCommand("select a.cn_name, a.en_name, a.content_length from OA_MAIL_RECEIVER r inner join OA_MAIL_ATTACHMENT a on r.mail_id = a.mail_id where r.id="+Request.QueryString["r_id"], conn);
  711.                     cmd.Transaction = tx;
  712.                     SqlDataReader dr = cmd.ExecuteReader();
  713.                     string cn_name = "";
  714.                     string en_name = "";
  715.                     string content_length = "";
  716.                     if (dr.Read())
  717.                     {
  718.                         cn_name = dr["cn_name"].ToString();
  719.                         en_name = dr["en_name"].ToString();
  720.                         content_length = dr["content_length"].ToString();
  721.                     }
  722.                     dr.Close();
  723.                     cmd = new SqlCommand("insert into OA_MAIL_ATTACHMENT(id, mail_id, cn_name, en_name, content_length)" +
  724.                         " values (" + attachment_id + ", " + mail_id + ", '" + cn_name + "','" + en_name + "','"+
  725.                         ContentLength.Value + "')", conn);
  726.                     cmd.Transaction = tx;
  727.                     cmd.ExecuteNonQuery();
  728.                 }
  729.             }
  730.             tx.Commit();
  731.             Response.Redirect("mail_list_send.aspx");
  732.         }
  733.         catch (Exception ex)
  734.         {
  735.             Response.Write(ex.Message);
  736.             tx.Rollback();
  737.         }
  738.         conn.Close();
  739.     }
  740. }