using DSWeb.Common.DB; using log4net; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace DSWeb.Common.Helper { /// /// 发送邮件辅助类 /// public static class MailHelper { private static ILog logger = LogManager.GetLogger("MailHelper"); /// /// 使用随机邮箱发送邮件 /// /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) public static bool SendMail(string title, string body, string sendTo) { bool succ = false; CommonDataContext commonDataContext = new CommonDataContext(); var smtpList = commonDataContext.MailSendSmtp.AsNoTracking().ToList(); MailSendSmtp smtp = null; if (smtpList.Count > 0) { Random rnd = new Random(); int idx = rnd.Next(smtpList.Count); smtp = smtpList[idx]; } MailSend mailSend = new MailSend(); mailSend.SendTo = sendTo; mailSend.Title = title; mailSend.Body = body; if (smtp != null) { mailSend.SendStatus = MailSend.MailSendStatusCreate; mailSend.SmtpConfig = smtp.GID; succ = true; } else { mailSend.SendStatus = MailSend.MailSendStatusFail; logger.Error("没有配置发送邮箱,邮件将不会发送"); } commonDataContext.MailSend.Add(mailSend); commonDataContext.SaveChanges(); return succ; } /// /// 使用指定的邮箱发送邮件 /// /// 指定smtp配置的id /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) /// 显示名称 /// 自定义发件人(只有小部分邮箱支持) public static bool SendMail(string configId, string title, string body, string sendTo, string showName = null, string sender = null) { bool succ = false; CommonDataContext commonDataContext = new CommonDataContext(); var smtp = commonDataContext.MailSendSmtp.AsNoTracking().FirstOrDefault(c => c.GID == configId); MailSend mailSend = new MailSend(); mailSend.SendTo = sendTo; mailSend.Title = title; mailSend.Body = body; mailSend.ShowName = showName; mailSend.Sender = sender; if (smtp != null) { mailSend.SendStatus = MailSend.MailSendStatusCreate; mailSend.SmtpConfig = smtp.GID; succ = true; } else { mailSend.SendStatus = MailSend.MailSendStatusFail; logger.Error("没有配置发送邮箱,邮件将不会发送"); } commonDataContext.MailSend.Add(mailSend); commonDataContext.SaveChanges(); return succ; } /// /// 使用默认发件地址发送邮件 /// /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) /// public static bool SendMailCommon(string title, string body, string sendTo) { return SendMail("4AB9E4EE-7E62-4E95-9168-F7CF0605E088", title, body, sendTo); } /// /// 使用马士基发件地址发送邮件 /// /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) /// public static bool SendMailMaersk(string title, string body, string sendTo) { return SendMail("37F7BF61-41F1-4B77-A1B8-41BAFEDF5AF1", title, body, sendTo); } /// /// 使用发票转发发件地址发送邮件 /// /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) /// public static bool SendMailInvoice(string title, string body, string sendTo) { return SendMail("4E07BC66-0E0A-4A97-A3B5-EC6516FAE24A", title, body, sendTo, "大简云发票平台"); } /// /// 发送发票邮件 /// /// 发票记录Id /// //public static bool SendMailInvoiceRecord(string invId) //{ // return SendMailInvoiceRecordOther(invId, null); //} /// /// 发送发票邮件 /// /// 发票记录Id /// 优先使用参数的邮箱,若参数未null时使用发票记录的邮箱 /// //public static bool SendMailInvoiceRecordOther(string invId, string email) //{ // InvoiceDbContext invDB = new InvoiceDbContext(); // CommonDataContext comDB = new CommonDataContext(); // var inv = invDB.Invoices.AsNoTracking().FirstOrDefault(i => i.GID == invId); // if (inv != null && inv.Status == InvoiceRecord.StatusIssued && (!string.IsNullOrEmpty(email) || !string.IsNullOrEmpty(inv.Email))) // { // var cfg = comDB.ConfigDatas.AsNoTracking().FirstOrDefault(cd => cd.Module == ConfigData.ModuleInvoice && cd.Category == ConfigData.CateMailSubject && cd.UserId == inv.UserId); // var title = ""; // if (cfg != null) // { // var subjectArr = JsonConvert.DeserializeObject(cfg.JsonData); // #region 标题拼接 // string mailTitle = string.Empty; // if (subjectArr.Contains("SalerName")) // { // mailTitle += "销售方:" + inv.SalerName; // } // if (subjectArr.Contains("Total")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // mailTitle += "金额:" + inv.Total; // } // if (subjectArr.Contains("InvoiceDate")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // mailTitle += "开票日期:" + inv.InvoiceDate.Value.ToString("yyyy年MM月dd日"); // } // if (subjectArr.Contains("InvNum")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // mailTitle += "发票号码:" + inv.InvNum; // } // if (subjectArr.Contains("InvoiceLine")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // switch (inv.InvoiceLine) // { // case "p": // mailTitle += "发票种类:普通发票(电票)"; // break; // case "c": // mailTitle += "发票种类:普通发票(纸票)"; // break; // case "s": // mailTitle += "发票种类:专用发票"; // break; // case "e": // mailTitle += "发票种类:收购发票(电票)"; // break; // case "f": // mailTitle += "发票种类:收购发票(纸质)"; // break; // } // } // if (subjectArr.Contains("Clerk")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // mailTitle += "开票人:" + inv.Clerk; // } // if (subjectArr.Contains("BuyerName")) // { // if (mailTitle.Length > 0) // { // mailTitle += "---"; // } // mailTitle += "购货方名称:" + inv.BuyerName; // } // #endregion // title = $"您收到一张电子发票:{mailTitle}"; // } // else // { // title = $"您收到一张电子发票:销售方:{inv.SalerName}---金额:{inv.Total}---开票日期:{inv.InvoiceDate.Value.ToString("yyyy年MM月dd日")}"; // } // //读取html模板,填充数据 // var htmlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Areas/Invoice/Content/InvoiceMailTemplate.html"); // var content = File.ReadAllText(htmlFile); // content = content.Replace("$VIEW_URL$", inv.PictureUrl); // content = content.Replace("$CUST_NAME$", inv.BuyerName); // content = content.Replace("$INV_DATA$", inv.InvoiceDate.Value.ToString("yyyy年MM月dd日")); // content = content.Replace("$TOTAL$", inv.Total.ToString()); // content = content.Replace("$INV_NUM$", inv.InvNum); // content = content.Replace("$INV_CODE$", inv.InvCode); // content = content.Replace("$PDF_URL$", inv.PdfUrl); // SendMailInvoice(title, content, string.IsNullOrEmpty(email) ? inv.Email : email); // return true; // } // return false; //} /// /// 发送邮件-运踪 /// /// 邮件标题 /// 邮件内容 /// 接收人(多个接收人用英文逗号或分号隔开) /// 发件人显示名称 /// 发件人显示邮箱地址(部分邮箱支持) /// public static bool SendMailBillTrace(string title, string body, string sendTo, string showName = null, string sender = null) { return SendMail("F48AA7593827456BB0DC12D4D8D13976", title, body, sendTo, showName, sender); } } }