using System.Collections.Specialized; using System.Text; using YieldChain.Helpers; using YLErp.DBModels.Consts; using YLErp.MailKit; namespace YLErp.Modules.AppModule { /// /// 邮件发送配置处理 /// class SmtpConfigHandler { readonly SmtpConfigExt _config; public SmtpConfigHandler(SmtpConfigExt config) { _config = config ?? throw new ArgumentNullException(nameof(config)); } /// /// 重置邮件发送配置 /// public void ResetConfig(string configText) { if (string.IsNullOrWhiteSpace(configText)) return; var arr = configText.Split(new[] { "[|]" }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length < 1) return; var nv = UrlHelper.ParseQueryString(arr[0]) ?? new NameValueCollection(); int.TryParse(nv["port"], out int port); _config.Server = nv["server"]; _config.Port = port > 0 ? port : 25; _config.UserName = nv["userName"]; _config.Password = nv["password"]; _config.EnableSsl = "true".Equals(nv["enableSsl"], StringComparison.OrdinalIgnoreCase); _config.ZipAttach = "true".Equals(nv["zipAttach"], StringComparison.OrdinalIgnoreCase); _config.Receipt = "true".Equals(nv["receipt"], StringComparison.OrdinalIgnoreCase); if (arr.Length < 2) return; var otherUsers = new List(arr.Length - 1); for (var i = 1; i < arr.Length; i++) { nv = UrlHelper.ParseQueryString(arr[i]); if (nv != null) { otherUsers.Add(new SmtpConfig { UserName = nv["userName"], Password = nv["password"], Receipt = "true".Equals(nv["receipt"], StringComparison.OrdinalIgnoreCase) }); } } _config.OtherUsers = otherUsers.ToArray(); } /// /// 重置邮件发送配置 /// public void ResetConfig(SmtpConfigExt newConfig, bool saveIntoDB) { if (newConfig is null) { throw new ArgumentNullException(nameof(newConfig)); } if (string.IsNullOrWhiteSpace(newConfig?.Server)) { throw new ServiceException("主机不能为空"); } if (string.IsNullOrWhiteSpace(newConfig.UserName) || newConfig.OtherUsers != null && newConfig.OtherUsers.Any(n => string.IsNullOrWhiteSpace(n.UserName))) { throw new ServiceException("用户不能为空"); } if (newConfig.OtherUsers != null && newConfig.OtherUsers.Count() + 1 != newConfig.OtherUsers.Select(n => n.UserName.Trim()).Concat(new[] { newConfig.UserName.Trim() }).ToHashSet(StringComparer.OrdinalIgnoreCase).Count) { throw new ServiceException("用户不能重复"); } if (saveIntoDB) { var sb = new StringBuilder(300); sb.Append("&server=").Append(newConfig.Server); sb.Append("&port=").Append(newConfig.Port); sb.Append("&userName=").Append(newConfig.UserName); sb.Append("&password=").Append(newConfig.Password); sb.Append("&enableSsl=").Append(newConfig.EnableSsl); sb.Append("&zipAttach=").Append(newConfig.ZipAttach); sb.Append("&receipt=").Append(newConfig.Receipt); if (newConfig.OtherUsers != null) { foreach (var u in newConfig.OtherUsers) { sb.Append("[|]"); sb.Append("&userName=").Append(u.UserName); sb.Append("&password=").Append(u.Password); sb.Append("&receipt=").Append(u.Receipt); } } var context = DbContextFactory.GetYLDbContext(); var config = context.AppConfig.FirstOrDefault(n => n.PGroup == ConsAppConfig.ProjectConfig && n.PName == ConsAppConfig.SendMailConfig); if (config == null) { config = new AppConfig { PGroup = ConsAppConfig.ProjectConfig, PName = ConsAppConfig.SendMailConfig, CreateTime = DateTime.Now, PType = "string", Remark = "邮件发送配置(格式:&server=192.168.1.1&port=25&userName=test@test.com&password=123456&enableSsl=false|true)" }; } config.PValue = sb.ToString(); context.SaveChanges(); } ObjectHelper.MapValues(_config, newConfig); } } }