Email格式: 加入notice_schedule和自動寄信前,增加檢查Email格式的判斷

This commit is contained in:
wanling040@gmail.com 2022-08-08 16:38:56 +08:00
parent 6125a31f3a
commit 494c8d4142
4 changed files with 265 additions and 199 deletions

View File

@ -12,6 +12,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Transactions; using System.Transactions;
using System.Text.RegularExpressions;
namespace SolarPower.Controllers namespace SolarPower.Controllers
{ {
@ -676,6 +677,8 @@ namespace SolarPower.Controllers
_ => "" _ => ""
}; };
var user = await userRepository.GetOneAsync(person); var user = await userRepository.GetOneAsync(person);
if (IsValidEmail(user.Email))
{
NoticeSchedule schedule = new NoticeSchedule() NoticeSchedule schedule = new NoticeSchedule()
{ {
EmailType = 3, EmailType = 3,
@ -688,6 +691,7 @@ namespace SolarPower.Controllers
}; };
noticeSchedules.Add(schedule); noticeSchedules.Add(schedule);
} }
}
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
@ -699,8 +703,11 @@ namespace SolarPower.Controllers
"Type", "Type",
"ExceptionId" "ExceptionId"
}; };
if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule"); await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
} }
}
if (post.FixFirm != 0) if (post.FixFirm != 0)
{ {
@ -714,6 +721,8 @@ namespace SolarPower.Controllers
}; };
//var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); //var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId);
var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm"); var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm");
if (IsValidEmail(firm.Email))
{
NoticeSchedule schedule = new NoticeSchedule() NoticeSchedule schedule = new NoticeSchedule()
{ {
EmailType = 3, EmailType = 3,
@ -723,6 +732,7 @@ namespace SolarPower.Controllers
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice, Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
}; };
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
@ -737,6 +747,7 @@ namespace SolarPower.Controllers
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule"); await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
} }
} }
}
//using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress)) //using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress))
//{ //{
@ -898,6 +909,8 @@ namespace SolarPower.Controllers
_ => "" _ => ""
}; };
var user = await userRepository.GetOneAsync(person); var user = await userRepository.GetOneAsync(person);
if (IsValidEmail(user.Email))
{
NoticeSchedule schedule = new NoticeSchedule() NoticeSchedule schedule = new NoticeSchedule()
{ {
EmailType = 3, EmailType = 3,
@ -910,6 +923,7 @@ namespace SolarPower.Controllers
}; };
noticeSchedules.Add(schedule); noticeSchedules.Add(schedule);
} }
}
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
@ -921,8 +935,11 @@ namespace SolarPower.Controllers
"Type", "Type",
"ExceptionId" "ExceptionId"
}; };
if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule"); await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
} }
}
if (post.FixFirm != 0) if (post.FixFirm != 0)
{ {
//var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId); //var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId);
@ -935,6 +952,8 @@ namespace SolarPower.Controllers
}; };
// var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); // var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId);
var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm"); var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm");
if (IsValidEmail(firm.Email))
{
NoticeSchedule schedule = new NoticeSchedule() NoticeSchedule schedule = new NoticeSchedule()
{ {
EmailType = 3, EmailType = 3,
@ -944,6 +963,7 @@ namespace SolarPower.Controllers
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice, Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
}; };
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
@ -956,8 +976,7 @@ namespace SolarPower.Controllers
"ExceptionId" "ExceptionId"
}; };
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule"); await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
}
} }
} }
@ -1325,5 +1344,11 @@ namespace SolarPower.Controllers
} }
} }
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
} }
} }

View File

@ -10,6 +10,7 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Quartz.Jobs namespace SolarPower.Quartz.Jobs
{ {
@ -248,6 +249,8 @@ namespace SolarPower.Quartz.Jobs
}; };
var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented)); var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented));
if (stationReportName != "") if (stationReportName != "")
{
if (IsValidEmail(user.Email))
{ {
NoticeSchedule DaySchedule = new NoticeSchedule() NoticeSchedule DaySchedule = new NoticeSchedule()
{ {
@ -272,6 +275,7 @@ namespace SolarPower.Quartz.Jobs
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
} }
} }
}
//綜合報表 每日 //綜合報表 每日
if (sentMaxpowerstations.Count != 0) if (sentMaxpowerstations.Count != 0)
{ {
@ -284,6 +288,8 @@ namespace SolarPower.Quartz.Jobs
Userid = user.Id Userid = user.Id
}; };
var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented)); var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented));
if (IsValidEmail(user.Email))
{
NoticeSchedule MaxSchedule = new NoticeSchedule() NoticeSchedule MaxSchedule = new NoticeSchedule()
{ {
UserId = user.Id, UserId = user.Id,
@ -305,6 +311,7 @@ namespace SolarPower.Quartz.Jobs
"Type" "Type"
}; };
await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties); await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
}
if (DateTime.Now.ToString("dd") == "01") if (DateTime.Now.ToString("dd") == "01")
{ {
@ -317,6 +324,8 @@ namespace SolarPower.Quartz.Jobs
Userid = user.Id Userid = user.Id
}; };
var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented)); var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented));
if (IsValidEmail(user.Email))
{
NoticeSchedule MaxmonthSchedule = new NoticeSchedule() NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
{ {
RecipientEmail = user.Email, RecipientEmail = user.Email,
@ -339,6 +348,7 @@ namespace SolarPower.Quartz.Jobs
}; };
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2); await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
} }
}
} }
@ -359,6 +369,8 @@ namespace SolarPower.Quartz.Jobs
}; };
var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented)); var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
if (stationReportmonthName != "") if (stationReportmonthName != "")
{
if (IsValidEmail(user.Email))
{ {
NoticeSchedule MonthSchedule = new NoticeSchedule() NoticeSchedule MonthSchedule = new NoticeSchedule()
{ {
@ -382,6 +394,7 @@ namespace SolarPower.Quartz.Jobs
}; };
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2); await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
} }
}
} }
@ -437,5 +450,11 @@ namespace SolarPower.Quartz.Jobs
return tempSerialNumber.ToString().Trim().PadRight(pad, '0'); return tempSerialNumber.ToString().Trim().PadRight(pad, '0');
} }
} }
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
} }
} }

View File

@ -9,6 +9,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net.Mail; using System.Net.Mail;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Quartz.Jobs namespace SolarPower.Quartz.Jobs
{ {
@ -43,6 +44,7 @@ namespace SolarPower.Quartz.Jobs
var updateNoticeSchedules = new List<NoticeSchedule>(); var updateNoticeSchedules = new List<NoticeSchedule>();
foreach (var notice in noticeSchedules) foreach (var notice in noticeSchedules)
{ {
if (IsValidEmail(notice.RecipientEmail)) {
var attachments = new List<string>(); var attachments = new List<string>();
var recipientEmails = new List<string>() var recipientEmails = new List<string>()
{ {
@ -84,6 +86,7 @@ namespace SolarPower.Quartz.Jobs
await noticeScheduleRepository.UpdateList(updateNoticeSchedules, properties); await noticeScheduleRepository.UpdateList(updateNoticeSchedules, properties);
} }
}
logger.LogInformation("【SendEmailJob】【任務完成】"); logger.LogInformation("【SendEmailJob】【任務完成】");
} }
@ -160,5 +163,12 @@ namespace SolarPower.Quartz.Jobs
} }
} }
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
} }
} }

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Repository.Implement namespace SolarPower.Repository.Implement
{ {
@ -647,17 +648,22 @@ namespace SolarPower.Repository.Implement
{ {
List<string> value = new List<string>(); List<string> value = new List<string>();
foreach (MyUser a in personal) foreach (MyUser a in personal)
{
if (IsValidEmail(a.Email))
{ {
value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id}, {emailType})"); value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id}, {emailType})");
} }
}
string values = string.Join(",", value.ToArray()); string values = string.Join(",", value.ToArray());
if (value.Count > 0)
{
var sql = $"INSERT INTO `notice_schedule` (`Type`, `RecipientName`, `RecipientEmail`, `Subject`, `Content`,`UserId`,`EmailType`) VALUES {values};"; var sql = $"INSERT INTO `notice_schedule` (`Type`, `RecipientName`, `RecipientEmail`, `Subject`, `Content`,`UserId`,`EmailType`) VALUES {values};";
await conn.ExecuteAsync(sql, trans); await conn.ExecuteAsync(sql, trans);
trans.Commit(); trans.Commit();
} }
}
catch (Exception exception) catch (Exception exception)
{ {
trans.Rollback(); trans.Rollback();
@ -797,5 +803,11 @@ namespace SolarPower.Repository.Implement
} }
} }
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
} }
} }