|
|
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
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 发送邮件辅助类
|
|
|
/// </summary>
|
|
|
public static class MailHelper
|
|
|
{
|
|
|
private static ILog logger = LogManager.GetLogger("MailHelper");
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用随机邮箱发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用指定的邮箱发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="configId">指定smtp配置的id</param>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <param name="showName">显示名称</param>
|
|
|
/// <param name="sender">自定义发件人(只有小部分邮箱支持)</param>
|
|
|
public static bool SendMail(string configId, string title, string body, string sendTo, string ccTo = null, string showName = null, string sender = null, bool customerSend = false, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
bool succ = false;
|
|
|
CommonDataContext commonDataContext = new CommonDataContext();
|
|
|
MailDataContext mailData = new MailDataContext();
|
|
|
|
|
|
MailSend mailSend = new MailSend();
|
|
|
mailSend.SendTo = sendTo;
|
|
|
mailSend.CCTo = ccTo;
|
|
|
mailSend.Title = title;
|
|
|
mailSend.Body = body;
|
|
|
mailSend.ShowName = showName;
|
|
|
mailSend.Sender = sender;
|
|
|
if (attachFiles != null && attachFiles.Length > 0)
|
|
|
{
|
|
|
mailSend.AttachFiles = JsonConvert.SerializeObject(attachFiles);
|
|
|
}
|
|
|
|
|
|
//使用大简云配置的smtp发邮件
|
|
|
if (!customerSend)
|
|
|
{
|
|
|
var smtp = commonDataContext.MailSendSmtp.AsNoTracking().FirstOrDefault(c => c.GID == configId);
|
|
|
if (smtp != null)
|
|
|
{
|
|
|
mailSend.SendStatus = MailSend.MailSendStatusCreate;
|
|
|
mailSend.SmtpConfig = smtp.GID;
|
|
|
succ = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
mailSend.SendStatus = MailSend.MailSendStatusFail;
|
|
|
logger.Error("没有配置发送邮箱,邮件将不会发送");
|
|
|
succ = false;
|
|
|
}
|
|
|
}
|
|
|
else //使用客户的邮箱发邮件
|
|
|
{
|
|
|
mailSend.CustomerSend = true;
|
|
|
var userMail = mailData.UserAccounts.AsNoTracking().FirstOrDefault(ua => ua.GID == configId);
|
|
|
if (userMail != null)
|
|
|
{
|
|
|
mailSend.SendStatus = MailSend.MailSendStatusCreate;
|
|
|
mailSend.SmtpConfig = userMail.GID;
|
|
|
succ = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
mailSend.SendStatus = MailSend.MailSendStatusFail;
|
|
|
logger.Error("未找到邮箱账号配置,邮件将不会发送");
|
|
|
succ = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
commonDataContext.MailSend.Add(mailSend);
|
|
|
commonDataContext.SaveChanges();
|
|
|
|
|
|
return succ;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用默认发件地址发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailCommon(string title, string body, string sendTo, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("4AB9E4EE-7E62-4E95-9168-F7CF0605E088", title, body, sendTo, attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用马士基发件地址发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailMaersk(string title, string body, string sendTo, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("37F7BF61-41F1-4B77-A1B8-41BAFEDF5AF1", title, body, sendTo, attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用发票转发发件地址发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailInvoice(string title, string body, string sendTo, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("4E07BC66-0E0A-4A97-A3B5-EC6516FAE24A", title, body, sendTo, showName: "大简云发票平台", attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用余额不足通知发件地址发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailService(string title, string body, string sendTo, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("0D6D86A5-B341-4317-94D7-435C868F780C", title, body, sendTo, attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 发送发票邮件
|
|
|
/// </summary>
|
|
|
/// <param name="invId">发票记录Id</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailInvoiceRecord(string invId)
|
|
|
{
|
|
|
return SendMailInvoiceRecordOther(invId, null);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 发送发票邮件
|
|
|
/// </summary>
|
|
|
/// <param name="invId">发票记录Id</param>
|
|
|
/// <param name="email">优先使用参数的邮箱,若参数未null时使用发票记录的邮箱</param>
|
|
|
/// <param name="cfgUserId">读取发件标题配置所属的用户,默认使用发票所有人的配置数据</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailInvoiceRecordOther(string invId, string email, string cfgUserId = null)
|
|
|
{
|
|
|
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 cfgUid = string.IsNullOrEmpty(cfgUserId) ? inv.UserId : cfgUserId;
|
|
|
var cfg = comDB.ConfigDatas.AsNoTracking().FirstOrDefault(cd => cd.Module == ConfigData.ModuleInvoice && cd.Category == ConfigData.CateMailSubject && cd.UserId == cfgUid);
|
|
|
var title = "";
|
|
|
if (cfg != null)
|
|
|
{
|
|
|
var subjectArr = JsonConvert.DeserializeObject<string[]>(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;
|
|
|
}
|
|
|
|
|
|
if (subjectArr.Contains("MBLNO"))
|
|
|
{
|
|
|
if (mailTitle.Length > 0)
|
|
|
{
|
|
|
mailTitle += "---";
|
|
|
}
|
|
|
mailTitle += "提单号:" + inv.MBLNO;
|
|
|
}
|
|
|
#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);
|
|
|
|
|
|
var att = new AttachFileModel()
|
|
|
{
|
|
|
FileName = $"{invId}.pdf",
|
|
|
FilePath = inv.PdfUrl
|
|
|
};
|
|
|
|
|
|
SendMailInvoice(title, content, string.IsNullOrEmpty(email) ? inv.Email : email, attachFiles: att);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 发送邮件-运踪
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <param name="showName">发件人显示名称</param>
|
|
|
/// <param name="sender">发件人显示邮箱地址(部分邮箱支持)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailBillTrace(string title, string body, string sendTo, string showName = null, string sender = null, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("F48AA7593827456BB0DC12D4D8D13976", title, body, sendTo, showName, sender, attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 转发邮件
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static bool TransmitMail(string configId, string sendTo, string emlFile, string dbConn = "DongShengDB")
|
|
|
{
|
|
|
CommonDataContext commonDataContext = new CommonDataContext(dbConn);
|
|
|
MailSend mailSend = new MailSend();
|
|
|
mailSend.SendTo = sendTo;
|
|
|
mailSend.Title = string.Empty;
|
|
|
mailSend.Body = string.Empty;
|
|
|
mailSend.CustomerSend = true;
|
|
|
mailSend.SendStatus = MailSend.MailSendStatusCreate;
|
|
|
mailSend.SmtpConfig = configId;
|
|
|
mailSend.EmlFile = emlFile;
|
|
|
mailSend.IsTransmit = true;
|
|
|
commonDataContext.MailSend.Add(mailSend);
|
|
|
commonDataContext.SaveChanges();
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用noreplay发件地址发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="title">邮件标题</param>
|
|
|
/// <param name="body">邮件内容</param>
|
|
|
/// <param name="sendTo">接收人(多个接收人用英文逗号或分号隔开)</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool SendMailNoReplay(string title, string body, string sendTo, params AttachFileModel[] attachFiles)
|
|
|
{
|
|
|
return SendMail("CAF0C46C-D85F-45E6-8383-25889818F2FC", title, body, sendTo, attachFiles: attachFiles);
|
|
|
}
|
|
|
|
|
|
//public static bool SendMailUseTemplate()
|
|
|
//{
|
|
|
// var template = commonData.MailSendTemplates.AsNoTracking().First(t => t.GID == "3CF45513-F996-4FBF-9A57-6E7851E389D8");
|
|
|
// Dictionary<string, string> dict = new Dictionary<string, string>();
|
|
|
// var propList = order.GetType().GetProperties();
|
|
|
// foreach (var prop in propList)
|
|
|
// {
|
|
|
// var valObj = prop.GetValue(order);
|
|
|
// if (valObj == null)
|
|
|
// {
|
|
|
// dict.Add($"ORDER.{prop.Name.ToUpper()}", string.Empty);
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// dict.Add($"ORDER.{prop.Name.ToUpper()}", valObj.ToString());
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
// var strRegex = "\\{\\{[A-Za-z0-9.]+\\}\\}";
|
|
|
|
|
|
// var title = template.Title;
|
|
|
// var mch = Regex.Match(title, strRegex);
|
|
|
// while (mch.Success)
|
|
|
// {
|
|
|
// var key = mch.Value.Replace("{{", "").Replace("}}", "").Trim().ToUpper();
|
|
|
// title = title.Replace(mch.Value, dict[key]);
|
|
|
|
|
|
// mch = Regex.Match(title, strRegex);
|
|
|
// }
|
|
|
|
|
|
// var body = template.Body;
|
|
|
// mch = Regex.Match(body, strRegex);
|
|
|
// while (mch.Success)
|
|
|
// {
|
|
|
// var key = mch.Value.Replace("{{", "").Replace("}}", "").Trim().ToUpper();
|
|
|
// body = body.Replace(mch.Value.ToUpper(), dict[key]);
|
|
|
|
|
|
// mch = Regex.Match(title, strRegex);
|
|
|
// }
|
|
|
//}
|
|
|
}
|
|
|
|
|
|
public class AttachFileModel
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 文件显示名称
|
|
|
/// </summary>
|
|
|
public string FileName { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 文件存放完整路径
|
|
|
/// </summary>
|
|
|
public string FilePath { get; set; }
|
|
|
}
|
|
|
} |