/// <summary> /// 异步发送邮件,并将发送记录备份 /// </summary> /// <param name="model">邮件信息模型</param> /// <param name="dataDT">不为空则对每个模板格式化后再发送</param> public static void SendAsync(M_EMail_Item model) { B_EMail_Item itemBll = new B_EMail_Item(); MailConfig cfg = SiteConfig.MailConfig; SendHandler sender = null; try { if (string.IsNullOrEmpty(model.FromEmail)) { model.FromEmail = cfg.MailServerUserName; } if (string.IsNullOrEmpty(model.FromName)) { model.FromName = SiteConfig.SiteInfo.Webmaster; } if (cfg.Port == 25) { sender = SendEmail; } else if (cfg.Port == 465) { sender = SendSSLEmail; } else { throw new Exception("邮件端口[" + cfg.Port + "]配置不正确"); } if (string.IsNullOrEmpty(cfg.MailServerUserName)) { throw new Exception("未配置发送邮箱,取消发送"); } if (string.IsNullOrEmpty(cfg.MailServerPassWord)) { throw new Exception("未配置发送邮箱密码,取消发送"); } model.Result = 1; model.Error = ""; } catch (Exception ex) { model.Result = -1; model.Error = ex.Message; return; } model.ID = itemBll.Insert(model); //----------------发送邮件(单个发送) string[] emails = model.ToAddress.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string mailBody = model.MailBody; for (int i = 0; i < emails.Length; i++) { M_EMail_Item mailMod = new M_EMail_Item() { ID = model.ID, ToAddress = emails[i], Subject = model.Subject, MailBody = mailBody, FromName = model.FromName, FromEmail = model.FromEmail, Attachment = model.Attachment }; //如果不需要每次循环时编译,应该在上层处理好再传入 if (mailMod.C_NeedTranslate) { DataRow dr = null; if (model.C_DataDT != null && model.C_DataDT.Rows.Count > i) { dr = model.C_DataDT.Rows[i]; } mailMod.MailBody = TlpDeal(mailBody, dr); } sender.BeginInvoke(cfg.MailServerUserName, cfg.MailServerPassWord, cfg.MailServer , mailMod, SendCallBack, null); } }
private static void SendSSLEmail(string account, string passwd, string server, M_EMail_Item mailMod) { M_EMail_SendLog logMod = new M_EMail_SendLog(); B_EMail_SendLog logBll = new B_EMail_SendLog(); logMod.ToAddress = mailMod.ToAddress; logMod.EmailID = mailMod.ID; //-------------------------------------------------- try { } catch (Exception ex) { logMod.Result = -1; logMod.ErrorMsg = ex.Message; } logBll.Insert(logMod); }
public bool UpdateByID(M_EMail_Item model) { return(DBCenter.UpdateByID(model, model.ID)); }
private static void SendEmail(string account, string passwd, string server, M_EMail_Item mailMod) { M_EMail_SendLog logMod = new M_EMail_SendLog(); B_EMail_SendLog logBll = new B_EMail_SendLog(); logMod.ToAddress = mailMod.ToAddress; logMod.EmailID = mailMod.ID; //-------------------------------------------------- SmtpClient client = new SmtpClient(server, 25); NetworkCredential credential = new NetworkCredential(account, passwd); client.UseDefaultCredentials = true; client.EnableSsl = false; client.Credentials = credential.GetCredential(server, 25, "Basic"); client.DeliveryMethod = SmtpDeliveryMethod.Network; //-------------------------------------------------- try { MailMessage mail = new MailMessage(); mail.SubjectEncoding = Encoding.UTF8; mail.BodyEncoding = Encoding.UTF8; mail.IsBodyHtml = true; mail.Priority = MailPriority.Normal; mail.Subject = mailMod.Subject; mail.Body = mailMod.MailBody; if (!string.IsNullOrEmpty(mailMod.FromName)) { mail.From = new MailAddress(account, mailMod.FromName); } else { mail.From = new MailAddress(account); } foreach (string file in mailMod.Attachment.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { mail.Attachments.Add(new Attachment(function.VToP(file))); } mail.To.Add(new MailAddress(mailMod.ToAddress)); //foreach (string email in mailMod.ToAddress.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) //{ // mail.To.Add(new MailAddress(email)); //} //foreach (string email in mailMod.CCAddress.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) //{ // mail.CC.Add(email); //} //foreach (string email in mailMod.BCCAddress.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) //{ // mail.Bcc.Add(email); //} client.Send(mail); logMod.Result = 1; } catch (Exception ex) { logMod.ErrorMsg = ex.Message; logMod.Result = -1; } logBll.Insert(logMod); }
public int Insert(M_EMail_Item model) { return(DBCenter.Insert(model)); }