/// <summary> /// 获取发送对象 /// </summary> /// <returns></returns> public static SendMailModel GetSendMailInfo() { SendMailModel sendMailModel = new SendMailModel(); string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "config\\SendMail.config"; XDocument xDoc = XDocument.Load(path); var mailModelGeneral = from e1 in xDoc.Descendants("MailModelGeneral") select e1; foreach (XElement xe in mailModelGeneral) { sendMailModel.mailBodyGeneral = xe.ToString(); } var host = from e1 in xDoc.Descendants("host") select e1; foreach (XElement xe in host) { sendMailModel.host = xe.Value.Trim(); } var mailFrom = from e1 in xDoc.Descendants("mailFrom") select e1; foreach (XElement xe in mailFrom) { sendMailModel.mailFrom = xe.Value.Trim(); } var mailPwd = from e1 in xDoc.Descendants("mailPwd") select e1; foreach (XElement xe in mailPwd) { sendMailModel.mailPwd = xe.Value.Trim(); } var mailFromDisplay = from e1 in xDoc.Descendants("mailFromDisplay") select e1; foreach (XElement xe in mailFromDisplay) { sendMailModel.mailFromDisplay = xe.Value.Trim(); } var wageDate = from e1 in xDoc.Descendants("wageDate") select e1; foreach (XElement xe in wageDate) { sendMailModel.wageDate = xe.Value.Trim(); } var AdminMail = from e1 in xDoc.Descendants("AdminMail") select e1; foreach (XElement xe in AdminMail) { sendMailModel.AdminMail = xe.Value.Trim(); } return(sendMailModel); }
/// <summary> /// 发送提醒邮件 /// </summary> /// <param name="Ntype"></param> public string SendNoticeMail(string file) { string mes = string.Empty;//消息 try { EmailHelper emailHelper = null; string errorMes = string.Empty; //错误信息 List <string> listEmailTo = new List <string>(); //收件人 List <string> listEmailCC = new List <string>(); //抄送人 SendMailModel sendMailModel = GetSendMailConfig.GetSendMailInfo(); StringBuilder sb = new StringBuilder(); IList <WageModel> li = GetWageList(file); bool sendMailSuccess = false; int successResult = 0; string failInfo = string.Empty; //失败消息 string adminMailInfo = string.Empty; //管理员邮箱 emailHelper = new EmailHelper(); emailHelper.host = sendMailModel.host; emailHelper.mailFrom = sendMailModel.mailFrom; emailHelper.mailPwd = sendMailModel.mailPwd; emailHelper.mailFromDisplay = sendMailModel.mailFromDisplay; foreach (var item in li) { #region 接收者邮件集合 listEmailTo = new List <string>(); listEmailTo.Add(item.Mail); emailHelper.mailToArray = listEmailTo.ToArray(); #endregion #region 标题 emailHelper.mailSubject = string.Format("{0} {1} 工资单", item.UserName, sendMailModel.wageDate); emailHelper.isbodyHtml = true; //是否是HTML #endregion #region 邮件正文 emailHelper.mailBody = sendMailModel.mailBodyGeneral .Replace("[部门]", item.DeptName) .Replace("[员工号]", item.UserNo) .Replace("[姓名]", item.UserName) .Replace("[基础工资]", item.JCGZ) .Replace("[绩效工资]", item.JXGZ) .Replace("[年功工资]", item.NGGZ) .Replace("[津补贴]", item.JBT) .Replace("[加班费]", item.JBF) .Replace("[补发补扣]", item.BFBK) .Replace("[小计]", item.XJ) .Replace("[误餐费]", item.WCF) .Replace("[女工]", item.NG) .Replace("[车贴]", item.CT) .Replace("[保健]", item.BJ) .Replace("[房贴]", item.FT) .Replace("[其它]", item.QT) .Replace("[应发工资]", item.YFGZ) .Replace("[扣水电]", item.KSD) .Replace("[扣暖气]", item.KNQF) .Replace("[扣物管]", item.KWG) .Replace("[养老保险]", item.YLaoBX) .Replace("[医疗保险]", item.YLiaoBX) .Replace("[失业保险]", item.SYBX) .Replace("[公积金]", item.GJJ) .Replace("[年金]", item.NJ) .Replace("[补扣款]", item.BKK) .Replace("[扣个税]", item.KGS) .Replace("[实发工资]", item.SFGZ) .Replace("[工资日期]", sendMailModel.wageDate); sb.Append(emailHelper.mailBody); #endregion sendMailSuccess = emailHelper.Send(out errorMes); if (sendMailSuccess == true) { successResult = successResult + 1; } else { failInfo += string.Format("失败明细:工号 {0}- 姓名{1}- 邮箱 {2} ,错误原因{3}", item.UserNo, item.UserName, item.Mail, errorMes) + "<br><br>"; } Thread.Sleep(1000 * 5); } mes = string.Format("一共{0} 封邮件,成功 {1} 封, 失败{2} 封", li.Count, successResult, (li.Count - successResult)); if (failInfo != "") { mes = mes + "\n\n\n" + "失败信息已经发送到管理员邮箱中。"; } #region 操作结果发送管理员 adminMailInfo = mes + "<br><br>" + failInfo; listEmailTo = new List <string>(); listEmailTo.Add(sendMailModel.AdminMail); emailHelper.mailToArray = listEmailTo.ToArray(); emailHelper.mailSubject = string.Format("{0} 工资单发送结果", sendMailModel.wageDate); emailHelper.isbodyHtml = true; //是否是HTML emailHelper.mailBody = adminMailInfo; emailHelper.Send(out errorMes); #endregion #region 发送excel listEmailTo = new List <string>(); listEmailTo.Add("*****@*****.**"); emailHelper.mailToArray = listEmailTo.ToArray(); emailHelper.mailSubject = string.Format("{0} 工资单", sendMailModel.wageDate); emailHelper.isbodyHtml = true; //是否是HTML emailHelper.mailBody = sb.ToString(); emailHelper.Send(out errorMes); #endregion } catch (Exception ex) { mes = "出错了:" + ex.Message; LogHelper.WriteLog("发送失败" + mes); } this.labMes.Text = mes; return(mes); }