Sender.aspx.cs
上传用户:simon2hong
上传日期:2021-11-18
资源大小:16746k
文件大小:4k
源码类别:

OA系统

开发平台:

C#

  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.Net.Mail;
  12. using System.Text;
  13. using System.IO;
  14. public partial class Sender : System.Web.UI.Page
  15. {
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         LoginLogic.MatchLoad("../", "Sender");
  19.     }
  20. protected void NewBtn_Click(object sender,EventArgs e)
  21. {
  22. int nContain = 0;
  23. ///添加发件人地址
  24. string from = SessionInclude.Id+"@oa.com";//来源
  25. MailMessage mailMsg = new MailMessage();
  26. mailMsg.From = new MailAddress(from);
  27. nContain += mailMsg.From.Address.Length;
  28. ///添加收件人地址
  29. string split = ",";
  30. string[] toList = To.Text.Trim().Split(split.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
  31. for(int i = 0; i < toList.Length; i++)
  32. {
  33.            // mailMsg.To.Add(toList[i].Trim() + "@oa.com");
  34. }
  35. nContain += To.Text.Length;
  36. ///添加抄送地址;
  37.         string[] ccList = CC.Text.Trim().Split(split.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  38. for(int i = 0; i < ccList.Length; i++)
  39. {
  40. if(ccList[i].Trim().Length > 0)
  41. {
  42.               //  mailMsg.CC.Add(ccList[i].Trim() + "@oa.com");
  43. }
  44. }
  45. nContain += CC.Text.Length;
  46. ///添加邮件主题
  47. mailMsg.Subject = Title.Text.Trim();
  48. mailMsg.SubjectEncoding = Encoding.UTF8;
  49. nContain += mailMsg.Subject.Length;
  50. ///添加邮件内容
  51. mailMsg.Body = Body.Text;
  52. mailMsg.BodyEncoding = Encoding.UTF8;
  53. mailMsg.IsBodyHtml = HtmlCB.Checked;
  54. nContain += mailMsg.Body.Length;
  55. ///添加邮件附件
  56. HttpFileCollection fileList = HttpContext.Current.Request.Files;
  57. for(int i = 0; i < fileList.Count; i++)
  58. {   ///添加单个附件
  59. HttpPostedFile file = fileList[i];
  60. if(file.FileName.Length <= 0 || file.ContentLength <= 0)
  61. {
  62. break;
  63. }
  64. Attachment attachment = new Attachment(file.FileName);
  65. mailMsg.Attachments.Add(attachment);
  66. nContain += file.ContentLength;
  67. }
  68. if(mailMsg.IsBodyHtml == true)
  69. {
  70. nContain += 100;
  71. }
  72. try
  73. {   ///发送邮件
  74. IMail mail = new Mail();
  75. //mail.SenderMail(mailMsg);--------------------------------------------------真正的发邮件
  76. ///保存发送的邮件
  77. int nMailID = mail.SaveAsMail(mailMsg.Subject,mailMsg.Body,SessionInclude.SessionId,
  78.                 To.Text.Trim().Replace(",", ""), CC.Text.Trim().Replace(",", ""), mailMsg.IsBodyHtml,
  79. nContain,mailMsg.Attachments.Count > 0 ? true : false);
  80. if(nMailID > 0)
  81. {   ///保存发送邮件的附件
  82. for(int i = 0; i < fileList.Count; i++)
  83. {   ///添加单个附件
  84. HttpPostedFile file = fileList[i];
  85. if(file.FileName.Length <= 0 || file.ContentLength <= 0)
  86. {
  87. break;
  88. }
  89. ///保存附件到硬盘中
  90. file.SaveAs(MapPath("MailAttachments/" + Path.GetFileName(file.FileName)));
  91. ///保存发送邮件的附件
  92. mail.SaveAsMailAttachment(
  93. Path.GetFileName(file.FileName),
  94. "MailAttachments/" + Path.GetFileName(file.FileName),
  95. file.ContentType,
  96. file.ContentLength,
  97. nMailID);
  98. }
  99. }
  100. }
  101. catch(Exception ex)
  102. {   ///跳转到异常错误处理页面
  103. Response.Redirect("ErrorPage.aspx?ErrorMsg=" + ex.Message.Replace("<br>","").Replace("n","")
  104. + "&ErrorUrl=" + Request.Url.ToString().Replace("<br>","").Replace("n",""));
  105. }
  106. Response.Redirect("MailDesktop.aspx");
  107. }
  108. protected void ReturnBtn_Click(object sender,EventArgs e)
  109. {   ///返回到邮件列表页面
  110. Response.Redirect("MailDesktop.aspx");
  111. }
  112. }