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

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_add_mail : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../", 2);
  17.     }
  18.     protected void btnOK_Click(object sender, EventArgs e)
  19.     {
  20.         int flag = 0;
  21.         if (Employee_ids.Value == "")
  22.         {
  23.             flag = 1;
  24.             Response.Write("<script language='javascript'>alert('收件人不能為空!');</script>");
  25.         }
  26.         if (TxtTitle.Text == "")
  27.         {
  28.             flag = 1;
  29.             Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
  30.         }
  31.         if (TxtContent.Text == "")
  32.         {
  33.             flag = 1;
  34.             Response.Write("<script language='javascript'>alert('郵件正文不能為空!');</script>");
  35.         }
  36.         if(flag == 0)
  37.         {
  38.             DateMgr date = new DateMgr();
  39.             string time = date.getTime();
  40.             //上傳檔案
  41.             if (!this.uploadFile(time))
  42.             {
  43.                 if (FileUpload1.HasFile)
  44.                 {
  45.                     //上傳檔案不成功
  46.                 }
  47.                 else
  48.                 {
  49.                     //沒有上傳附件
  50.                     this.saveMail();
  51.                 }
  52.             }
  53.             else
  54.             {
  55.                 this.saveMail();
  56.             }
  57.         }
  58.     }
  59.     protected void saveMail()
  60.     {
  61.         //添加數据
  62.         SqlConnection conn = dbConnection.getConnection();
  63.         conn.Open();
  64.         int mail_id = 1;
  65.         SqlCommand cmd = new SqlCommand("select max(id) from OA_MAIL_CONTENT", conn);
  66.         try
  67.         {
  68.             mail_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  69.         }
  70.         catch { }
  71.         int sender_id = 1;
  72.         cmd = new SqlCommand("select max(id) from OA_MAIL_SENDER", conn);
  73.         try
  74.         {
  75.             sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  76.         }
  77.         catch { }
  78.         int receiver_id = 1;
  79.         cmd = new SqlCommand("select max(id) from OA_MAIL_RECEIVER", conn);
  80.         try
  81.         {
  82.             receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  83.         }
  84.         catch { }
  85.         int attachment_id = 1;
  86.         cmd = new SqlCommand("select max(id) from OA_MAIL_ATTACHMENT", conn);
  87.         try
  88.         {
  89.             attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  90.         }
  91.         catch { }
  92.         SqlTransaction tx = conn.BeginTransaction();
  93.         try
  94.         {
  95.             //添加郵件內容
  96.             cmd = new SqlCommand("insert into OA_MAIL_CONTENT(id, title, content) values (" +
  97.                 mail_id + ", '" + TxtTitle.Text + "', '" + TxtContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "')", conn);
  98.             cmd.Transaction = tx;
  99.             cmd.ExecuteNonQuery();
  100.             //添加發件人信息
  101.             int user_id = 0;
  102.             if (Session["user"] != null)
  103.             {
  104.                 user_id = ((User)Session["user"]).Id;
  105.             } 
  106.             DateMgr mgr = new DateMgr();
  107.             string time = mgr.getDateTime();
  108.             cmd = new SqlCommand("insert into OA_MAIL_SENDER(id, mail_id, sender, state, " +
  109.                 "is_del, send_date, cre_date)values(" + sender_id + ", " + mail_id + ", " + user_id +
  110.                 ", 1, 0, '" + time + "', '" + time + "')", conn);
  111.             cmd.Transaction = tx;
  112.             cmd.ExecuteNonQuery();
  113.             //添加收件人
  114.             if (!Employee_ids.Value.Equals(""))
  115.             {
  116.                 string[] receiver_ids = Employee_ids.Value.Split(new char[] {';'});
  117.                 for (int i = 0; i < receiver_ids.Length; i++)
  118.                 {
  119.                     cmd = new SqlCommand("insert into OA_MAIL_RECEIVER(id, mail_id, " +
  120.                         "receiver, is_read, is_del, read_date) values (" + receiver_id + ", " +
  121.                         mail_id + ", " + receiver_ids[i] + ", 0, 0, '')", conn);
  122.                     cmd.Transaction = tx;
  123.                     cmd.ExecuteNonQuery();
  124.                     receiver_id++;
  125.                 }
  126.             }
  127.             //添加附件
  128.             if (!FileUpload1.FileName.Equals(""))
  129.             {
  130.                 DateMgr date = new DateMgr();
  131.                 time = date.getTime();
  132.                 string name = "";
  133.                 try
  134.                 {
  135.                     name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  136.                 }
  137.                 catch { }
  138.                 cmd = new SqlCommand("insert into OA_MAIL_ATTACHMENT(id, mail_id, cn_name, en_name, content_length)" +
  139.                     " values (" + attachment_id + ", " + mail_id + ", '"+FileUpload1.FileName+"', '" + name + "','" +
  140.                     ContentLength.Value + "')", conn);
  141.                 cmd.Transaction = tx;
  142.                 cmd.ExecuteNonQuery();
  143.             }
  144.             tx.Commit();
  145.             Response.Redirect("mail_list_send.aspx");
  146.         }
  147.         catch (Exception ex)
  148.         {
  149.             Response.Write(ex.Message);
  150.             //tx.Rollback();
  151.         }
  152.         conn.Close();
  153.     }
  154.     protected bool uploadFile(string time)
  155.     {
  156.         bool flag = false;
  157.         string directory = "e:\oa\web\oa_upload\mail\";
  158.         if (FileUpload1.HasFile)
  159.         {
  160.             bool extenstion = false;
  161.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  162.             string[] allowedExtensions = 
  163.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip",".pdf", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  164.             for (int i = 0; i < allowedExtensions.Length; i++ )
  165.             {
  166.                 if(fileExtension == allowedExtensions[i])
  167.                 {
  168.                     extenstion = true;
  169.                     break;
  170.                 }
  171.             }
  172.             if (!extenstion)
  173.             {
  174.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg,pdf, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  175.                 return false;
  176.             }
  177.             else 
  178.             {
  179.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  180.                 
  181.                 FileUpload1.PostedFile.SaveAs(directory + name);
  182.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  183.                 flag = true;
  184.             }
  185.         }
  186.         return flag;
  187.     }
  188. }