add_mail.aspx.cs
资源名称:web.rar [点击查看]
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:7k
源码类别:
OA系统
开发平台:
ASP/ASPX
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- public partial class web_mail_add_mail : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- LoginDAO.CheckLogin(Session, Response, "../", 2);
- }
- protected void btnOK_Click(object sender, EventArgs e)
- {
- int flag = 0;
- if (Employee_ids.Value == "")
- {
- flag = 1;
- Response.Write("<script language='javascript'>alert('收件人不能為空!');</script>");
- }
- if (TxtTitle.Text == "")
- {
- flag = 1;
- Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
- }
- if (TxtContent.Text == "")
- {
- flag = 1;
- Response.Write("<script language='javascript'>alert('郵件正文不能為空!');</script>");
- }
- if(flag == 0)
- {
- DateMgr date = new DateMgr();
- string time = date.getTime();
- //上傳檔案
- if (!this.uploadFile(time))
- {
- if (FileUpload1.HasFile)
- {
- //上傳檔案不成功
- }
- else
- {
- //沒有上傳附件
- this.saveMail();
- }
- }
- else
- {
- this.saveMail();
- }
- }
- }
- protected void saveMail()
- {
- //添加數据
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- int mail_id = 1;
- SqlCommand cmd = new SqlCommand("select max(id) from OA_MAIL_CONTENT", conn);
- try
- {
- mail_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- int sender_id = 1;
- cmd = new SqlCommand("select max(id) from OA_MAIL_SENDER", conn);
- try
- {
- sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- int receiver_id = 1;
- cmd = new SqlCommand("select max(id) from OA_MAIL_RECEIVER", conn);
- try
- {
- receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- int attachment_id = 1;
- cmd = new SqlCommand("select max(id) from OA_MAIL_ATTACHMENT", conn);
- try
- {
- attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- SqlTransaction tx = conn.BeginTransaction();
- try
- {
- //添加郵件內容
- cmd = new SqlCommand("insert into OA_MAIL_CONTENT(id, title, content) values (" +
- mail_id + ", '" + TxtTitle.Text + "', '" + TxtContent.Text.Replace("n", "<br>").Replace(" ", " ") + "')", conn);
- cmd.Transaction = tx;
- cmd.ExecuteNonQuery();
- //添加發件人信息
- int user_id = 0;
- if (Session["user"] != null)
- {
- user_id = ((User)Session["user"]).Id;
- }
- DateMgr mgr = new DateMgr();
- string time = mgr.getDateTime();
- cmd = new SqlCommand("insert into OA_MAIL_SENDER(id, mail_id, sender, state, " +
- "is_del, send_date, cre_date)values(" + sender_id + ", " + mail_id + ", " + user_id +
- ", 1, 0, '" + time + "', '" + time + "')", conn);
- cmd.Transaction = tx;
- cmd.ExecuteNonQuery();
- //添加收件人
- if (!Employee_ids.Value.Equals(""))
- {
- string[] receiver_ids = Employee_ids.Value.Split(new char[] {';'});
- for (int i = 0; i < receiver_ids.Length; i++)
- {
- cmd = new SqlCommand("insert into OA_MAIL_RECEIVER(id, mail_id, " +
- "receiver, is_read, is_del, read_date) values (" + receiver_id + ", " +
- mail_id + ", " + receiver_ids[i] + ", 0, 0, '')", conn);
- cmd.Transaction = tx;
- cmd.ExecuteNonQuery();
- receiver_id++;
- }
- }
- //添加附件
- if (!FileUpload1.FileName.Equals(""))
- {
- DateMgr date = new DateMgr();
- time = date.getTime();
- string name = "";
- try
- {
- name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- }
- catch { }
- cmd = new SqlCommand("insert into OA_MAIL_ATTACHMENT(id, mail_id, cn_name, en_name, content_length)" +
- " values (" + attachment_id + ", " + mail_id + ", '"+FileUpload1.FileName+"', '" + name + "','" +
- ContentLength.Value + "')", conn);
- cmd.Transaction = tx;
- cmd.ExecuteNonQuery();
- }
- tx.Commit();
- Response.Redirect("mail_list_send.aspx");
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- //tx.Rollback();
- }
- conn.Close();
- }
- protected bool uploadFile(string time)
- {
- bool flag = false;
- string directory = "e:\oa\web\oa_upload\mail\";
- if (FileUpload1.HasFile)
- {
- bool extenstion = false;
- string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- string[] allowedExtensions =
- { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip",".pdf", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
- for (int i = 0; i < allowedExtensions.Length; i++ )
- {
- if(fileExtension == allowedExtensions[i])
- {
- extenstion = true;
- break;
- }
- }
- if (!extenstion)
- {
- Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg,pdf, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
- return false;
- }
- else
- {
- string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- FileUpload1.PostedFile.SaveAs(directory + name);
- ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
- flag = true;
- }
- }
- return flag;
- }
- }