/// <summary> /// 开启电子邮件发射之旅 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMailSend_Click(object sender, EventArgs e) { if (lstRecipients.Items.Count <= 0) { MsgBox.Show("收件人信息不能为空。"); return; } //SMTP 服务器信息 var _server = txtSMTPServer.Text.Trim(); var _port = TypeHelper.TypeToInt32(txtSMTPPort.Text.Trim(), 25); var _user = txtSMTPUser.Text.Trim(); var _pwd = txtSMTPPwd.Text.Trim(); //服务器信息 EMailSrvInfo _srvInfo = new EMailSrvInfo(); _srvInfo.SMTPServer = _server; _srvInfo.SMTPPort = _port; _srvInfo.UserName = _user; _srvInfo.Password = _pwd; _srvInfo.DisplayName = txtMailUser.Text.Trim(); //电子邮件信息 EMailInfo _mailInfo = new EMailInfo(); foreach (string item in lstRecipients.Items) { _mailInfo.Recipient.Enqueue(item); } _mailInfo.Subject = txtMailSubject.Text.Trim(); _mailInfo.IsHTML = chkMailHtml.Checked; _mailInfo.Body = txtMailBody.Text.Trim(); EMailHelper _mailHelper = new EMailHelper(_srvInfo); _mailHelper.Send(_mailInfo); if (_mailHelper.IsSuccessful) { MsgBox.Show("电子邮件发送成功!"); } else { MsgBox.Show($"电子邮件发送失败!【原因:{_mailHelper.LastErrorMessage}】"); } }
public static void SendMail(Exception e, string Sql) { Flow.Model.EMailInfo info = new Flow.Model.EMailInfo(); info.Address = ""; string admin_mail = System.Configuration.ConfigurationSettings.AppSettings["admin_mail"]; string Urls = System.Configuration.ConfigurationSettings.AppSettings["URL"]; string SqlStr = "select empid from workuser(Nolock) where formid=@FormId"; SqlParameter[] parats = { new SqlParameter("@FormId",SqlDbType.Int)}; parats[0].Value = 0; using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionWorkFlow, CommandType.Text, SqlStr, parats)) { DataTable tab = new DataTable(); tab.Load(reader); for (int i = 0; i < tab.Rows.Count; i++) { info.Address += tab.Rows[i]["empid"].ToString().Trim() + "@sz.unimicron.com;"; } info.CopyAddress = ""; string pcid = ""; string pc = ""; HttpCookie MyCookie = HttpContext.Current.Request.Cookies["UserInfo"]; if (MyCookie != null) { pcid = MyCookie["login"].ToString(); pc = MyCookie["comp"].ToString(); } info.HeadNote = "【系統異常】:請管理員及時處理!!!" + " 用户名:" + HttpContext.Current.User.Identity.Name + " 電腦登陸ID:" + pcid + " 電腦號:" + pc; info.Note = "【異常原因】:" + e.ToString().Substring(0, 300) + "\n"; string url = GetUrl(); url = url.Replace("https", "http"); info.Note += "【引發頁面】:" + url + "\n"; info.Note += "【引發語句】:" + Sql; SendEMail(info); if (url.IndexOf("FormFlowBottom.aspx") == -1) { int index = url.ToLower().IndexOf("flowweb"); if (e.ToString().IndexOf("已超過連接逾時的設定。在作業完成之前超過逾時等待的時間") > -1 || e.ToString().IndexOf("處理序鎖死") > -1) { url = url.Substring(0, index + 8) + "Error.aspx?ExId=1"; } else if (e.ToString().IndexOf("記錄檔檔案已滿") > -1) { url = url.Substring(0, index + 8) + "Error.aspx?ExId=2"; } else url = url.Substring(0, index + 8) + "Error.aspx?ExId=0"; System.Web.HttpContext.Current.Response.Redirect(url); System.Web.HttpContext.Current.Response.End(); } else { string StrNum = url; int begin = StrNum.IndexOf("formid="); int begin2 = StrNum.IndexOf("&paperno="); int begin3 = StrNum.IndexOf("&key"); string FormID = ""; string PaperNO = ""; if (begin > -1 && begin2 > begin) { FormID = StrNum.Substring(begin + 7, begin2 - begin - 7); } if (begin3 > -1 && begin3 > begin2) { PaperNO = StrNum.Substring(begin2 + 9, begin3 - begin2 - 9); } if (begin3 == -1) { PaperNO = StrNum.Substring(begin2 + 9); } //插入異常表 try { string StrSql2 = "if not exists(select paperno from ExceptionTable(nolock) where formid=@FormId and paperno=@PaperNo) insert into ExceptionTable(FormId,PaperNo,ExceptionType,ExTime) values(@FormId,@PaperNo,@ExceptionType,getdate())"; SqlParameter[] parameterss = { new SqlParameter("@FormId", SqlDbType.VarChar,20), new SqlParameter("@PaperNo", SqlDbType.VarChar,20), new SqlParameter("@ExceptionType", SqlDbType.VarChar,225) }; parameterss[0].Value = FormID == null ? "" : FormID; parameterss[1].Value = PaperNO == null ? "" : PaperNO; parameterss[2].Value = e.ToString().Length < 200 ? e.ToString() : e.ToString().Substring(0, 200); SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionWorkFlow, CommandType.Text, StrSql2, parameterss); } catch (Exception exc) { EMailInfo emailinfoA = new EMailInfo(); emailinfoA.Address = admin_mail; emailinfoA.HeadNote = "workflow異常通知:添加異常表失敗!"; emailinfoA.Note = "【單據編號】:" + PaperNO + ((char)10).ToString(); emailinfoA.Note += "【FormId】:" + FormID + ((char)10).ToString(); emailinfoA.Note += "【異常原因】:" + exc.ToString() + ((char)10).ToString() + "\n"; emailinfoA.Note += "【查閱單據】: " + Urls + "/workflow/FormFlow.aspx?formid=" + FormID + "&paperno=" + PaperNO; SendEMail(emailinfoA); } //取出表單名稱 string StrSql = "select FormName from SEC_ClassForm(Nolock) where formid=@FormID"; SqlParameter[] paratsss = { new SqlParameter("@FormID",SqlDbType.Int) }; paratsss[0].Value = FormID; object obj = SqlHelper.ExecuteScalar(SqlHelper.ConnectionWorkFlow, CommandType.Text, StrSql, paratsss); string TableName = obj.ToString(); //取出填單人 string StrSql1 = "select PaperEmpId from SEC_FormFlowBas(NOLOCK) where FormID=@FormID and PaperNO=@PaperNO"; string Kyer = ""; SqlParameter[] paratss = { new SqlParameter("@FormID",SqlDbType.Int), new SqlParameter("@PaperNO",SqlDbType.VarChar,20) }; paratss[0].Value = FormID; paratss[1].Value = PaperNO; SqlDataReader readers = SqlHelper.ExecuteReader(SqlHelper.ConnectionWorkFlow, CommandType.Text, StrSql1, paratss); DataTable table = new DataTable(); table.Load(readers); if (table.Rows.Count > 0) //Kyer = table.Rows[0]["AppEmpId"].ToString().Replace(" ", "") + "@sz.unimicron.com;"; Kyer = HttpContext.Current.User.Identity.Name; else Kyer = "[email protected];"; //取出管理員 EMailInfo emailinfo = new EMailInfo(); if (FormID == "5" || FormID == "9" || FormID == "56" || FormID == "57") { emailinfo.Address = "[email protected];[email protected];[email protected];[email protected]"; } else { emailinfo.Address = admin_mail + ";" + Kyer; emailinfo.CopyAddress = "[email protected];[email protected];";//加入資訊主管窗口 } emailinfo.HeadNote = "workflow異常通知:" + TableName + ":" + PaperNO + "拋入ERP失敗!"; emailinfo.Note = "【單據編號】:" + PaperNO + ((char)10).ToString(); emailinfo.Note += "【表單名稱】:" + TableName + ((char)10).ToString(); string ExString = ""; if (e.ToString().Length > 200) ExString = e.ToString().Substring(0, 200); else ExString = e.ToString(); emailinfo.Note += "【異常原因】:" + ExString + "\n"; emailinfo.Note += "【查閱單據】: " + Urls + "/workflow/FormFlow.aspx?formid=" + FormID + "&paperno=" + PaperNO; SendEMail(emailinfo); //判斷異常原因 if (e.ToString().IndexOf("已超過連接逾時的設定。在作業完成之前超過逾時等待的時間") > -1) { int index = url.ToLower().IndexOf("flowweb"); url = url.Substring(0, index + 8) + "Error2.aspx"; System.Web.HttpContext.Current.Response.Redirect(url); } } } }