public async Task <IActionResult> DelEmail(int id) { TargetEmailBll targetEmailBll = new TargetEmailBll(); var info = await targetEmailBll.Get(id); await targetEmailBll.DelAsync(info); return(Json(new { code = 1, msg = "OK" })); }
/// <summary> /// 目标邮箱 /// </summary> /// <param name="pageIndex"></param> /// <returns></returns> public async Task <IActionResult> TargetEmailList(int pageIndex = 1) { int pageSize = 20; TargetEmailBll targetEmailBll = new TargetEmailBll(); var tup = await targetEmailBll.GetList(pageIndex, pageSize); ViewBag.count = tup.Item2; ViewBag.pageSize = pageSize; ViewBag.pageIndex = pageIndex; ViewBag.root = "email"; return(View(tup.Item1)); }
public async Task <IActionResult> uploadTagEmail() { JsonResult <PicMsg> result = new JsonResult <PicMsg>(); result.code = 1; result.msg = ""; // 获取附带POST参数值 for (var fileId = 0; fileId < Request.Form.Files.Count; fileId++) { var curFile = Request.Form.Files[fileId]; if (curFile.Length < 1) { continue; } var fileExt = Path.GetExtension(curFile.FileName); if (String.IsNullOrEmpty(fileExt) || fileExt != ".xlsx") { result.msg = "上传文件中包含不支持文件格式!"; return(Json(result)); } else { // 存储文件名 string FileName = DateTime.Now.ToString("yyyyMMddhhmmssff") + CreateRandomCode(8) + fileExt; // 存储路径(绝对路径) string virtualPath = Path.Combine(hostingEnv.WebRootPath, "file"); if (!Directory.Exists(virtualPath)) { Directory.CreateDirectory(virtualPath); } string filepath = Path.Combine(virtualPath, FileName); try { using (FileStream fs = System.IO.File.Create(filepath)) { await curFile.CopyToAsync(fs); fs.Flush(); } FileInfo file = new FileInfo(filepath); using (ExcelPackage package = new ExcelPackage(file)) { StringBuilder sb = new StringBuilder(); ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int rowCount = worksheet.Dimension.Rows; int ColCount = worksheet.Dimension.Columns; if (rowCount < 2 || ColCount < 2) { result.msg = "Excel模板不正确"; return(Json(result)); } StringBuilder stringBuilder = new StringBuilder(); Dictionary <string, int> enteridDIc = new Dictionary <string, int>(); TargetEmailBll bll = new TargetEmailBll(); for (int row = 2; row <= rowCount; row++) { TargetEmail targetEmail = new TargetEmail(); for (int col = 1; col <= ColCount; col++) { string value = worksheet.Cells[row, col].Value.TryToString().Trim(); switch (col) { case 1: targetEmail.Name = value; break; case 2: targetEmail.Email = value; break; } } bool flag = await bll.Exists(targetEmail.Name); if (flag) { stringBuilder.AppendLine(string.Format("{0}已存在", targetEmail.Name)); continue; } int id = await bll.AddAsync(targetEmail); if (id <= 0) { stringBuilder.AppendLine(string.Format("邮箱【{0}】|添加失败", targetEmail.Name)); } } if (stringBuilder.Length > 0) { string errorfile = DateTime.Now.ToString("yyyyMMddhhmmssff") + "error.txt"; string errorfilePath = Path.Combine(virtualPath, errorfile); try { FileStream filestream = new FileStream(errorfilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); StreamWriter writer = new StreamWriter(filestream, System.Text.Encoding.UTF8); writer.BaseStream.Seek(0, SeekOrigin.End); await writer.WriteAsync(stringBuilder.ToString()); writer.Flush(); writer.Close(); filestream.Close(); result.data = new PicMsg(); result.data.src = "/file/" + errorfile; result.data.title = errorfile; } catch (Exception ex) { ClassLoger.Error("uploadTagEmail", ex); } } } result.code = 0; result.msg = "OK"; } catch (Exception exception) { result.msg = "上传失败:" + exception.Message; } } } return(Json(result)); }
public async Task <IActionResult> SaveSendEmailTask() { SendEmailTask sendEmailTask = new SendEmailTask(); SendEmailLogBll sendEmailLogBll = new SendEmailLogBll(); sendEmailTask.EmailTempId = Request.Form["EmailTempId"].TryToInt(0); sendEmailTask.TaskName = Request.Form["TaskName"].TryToString(); sendEmailTask.CreateTime = DateTime.Now; string Email = Request.Form["Email"].TryToString(); bool flag = Request.Form["sendAll"].TryToString() == "on"; SendEmailTaskBll bll = new SendEmailTaskBll(); int id = await bll.AddAsync(sendEmailTask); if (id > 0) { Task.Run(async() => { if (!Email.IsNull()) { SendEmailLog log = new SendEmailLog { SendEmailTaskId = id, Email = Email, IsRead = false, IsSend = false, IsSendOk = false, Name = Email, EmailTempId = sendEmailTask.EmailTempId, }; await sendEmailLogBll.AddAsync(log); } if (flag) { TargetEmailBll targetEmailBll = new TargetEmailBll(); int pageIndex = 0; int pageSize = 50; A: pageIndex++; var tup = await targetEmailBll.GetList(pageIndex, pageSize); if (tup.Item1 != null && tup.Item1.Count > 0) { foreach (TargetEmail targetEmail in tup.Item1) { SendEmailLog log1 = new SendEmailLog { SendEmailTaskId = id, Email = targetEmail.Email, IsRead = false, IsSend = false, IsSendOk = false, Name = targetEmail.Name, EmailTempId = sendEmailTask.EmailTempId, }; await sendEmailLogBll.AddAsync(log1); } goto A; } } SendEmailHelper.StartSendEmail(id); }); } return(Json(new { code = 1, msg = "OK" })); }