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,30 +677,36 @@ namespace SolarPower.Controllers
_ => "" _ => ""
}; };
var user = await userRepository.GetOneAsync(person); var user = await userRepository.GetOneAsync(person);
NoticeSchedule schedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
EmailType = 3, NoticeSchedule schedule = new NoticeSchedule()
Type = 1, {
UserId = person, EmailType = 3,
RecipientName = user.Name, Type = 1,
RecipientEmail = user.Email, UserId = person,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, RecipientName = user.Name,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice, RecipientEmail = user.Email,
}; Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
noticeSchedules.Add(schedule); Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
}
} }
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
"EmailType", "EmailType",
"RecipientEmail", "RecipientEmail",
"Subject", "Subject",
"Content", "Content",
"RecipientName", "RecipientName",
"Type", "Type",
"ExceptionId" "ExceptionId"
}; };
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule"); if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
}
} }
if (post.FixFirm != 0) if (post.FixFirm != 0)
@ -714,27 +721,31 @@ 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");
NoticeSchedule schedule = new NoticeSchedule() if (IsValidEmail(firm.Email))
{ {
EmailType = 3, NoticeSchedule schedule = new NoticeSchedule()
Type = 1, {
RecipientName = firm.Name, EmailType = 3,
RecipientEmail = firm.Email, Type = 1,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, RecipientName = firm.Name,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice, RecipientEmail = firm.Email,
}; Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
properties = new List<string>() Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
{ };
"UserId",
"EmailType", properties = new List<string>()
"RecipientEmail", {
"Subject", "UserId",
"Content", "EmailType",
"RecipientName", "RecipientEmail",
"Type", "Subject",
"ExceptionId" "Content",
}; "RecipientName",
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule"); "Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
}
} }
} }
//using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress)) //using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress))
@ -898,30 +909,36 @@ namespace SolarPower.Controllers
_ => "" _ => ""
}; };
var user = await userRepository.GetOneAsync(person); var user = await userRepository.GetOneAsync(person);
NoticeSchedule schedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
EmailType = 3, NoticeSchedule schedule = new NoticeSchedule()
Type = 1, {
UserId = person, EmailType = 3,
RecipientName = user.Name, Type = 1,
RecipientEmail = user.Email, UserId = person,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, RecipientName = user.Name,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice, RecipientEmail = user.Email,
}; Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
noticeSchedules.Add(schedule); Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
}
} }
properties = new List<string>() properties = new List<string>()
{ {
"UserId", "UserId",
"EmailType", "EmailType",
"RecipientEmail", "RecipientEmail",
"Subject", "Subject",
"Content", "Content",
"RecipientName", "RecipientName",
"Type", "Type",
"ExceptionId" "ExceptionId"
}; };
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule"); if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
}
} }
if (post.FixFirm != 0) if (post.FixFirm != 0)
{ {
@ -935,29 +952,31 @@ 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");
NoticeSchedule schedule = new NoticeSchedule() if (IsValidEmail(firm.Email))
{ {
EmailType = 3, NoticeSchedule schedule = new NoticeSchedule()
Type = 1, {
RecipientName = firm.Name, EmailType = 3,
RecipientEmail = firm.Email, Type = 1,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, RecipientName = firm.Name,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice, RecipientEmail = firm.Email,
}; Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
properties = new List<string>() Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
{ };
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
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
{ {
@ -249,27 +250,30 @@ 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 != "")
{ {
NoticeSchedule DaySchedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
UserId = user.Id, NoticeSchedule DaySchedule = new NoticeSchedule()
EmailType = 0, {
RecipientEmail = user.Email, UserId = user.Id,
Subject = "日報表", EmailType = 0,
Attachment = stationReportName, RecipientEmail = user.Email,
RecipientName = user.Name, Subject = "日報表",
Type = 1 Attachment = stationReportName,
}; RecipientName = user.Name,
List<string> properties = new List<string>() Type = 1
{ };
"UserId", List<string> properties = new List<string>()
"EmailType", {
"RecipientEmail", "UserId",
"Subject", "EmailType",
"Attachment", "RecipientEmail",
"RecipientName", "Subject",
"Type" "Attachment",
}; "RecipientName",
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); "Type"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
} }
} }
//綜合報表 每日 //綜合報表 每日
@ -284,17 +288,19 @@ 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));
NoticeSchedule MaxSchedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
UserId = user.Id, NoticeSchedule MaxSchedule = new NoticeSchedule()
EmailType = 2, {
RecipientEmail = user.Email, UserId = user.Id,
Subject = "綜合報表", EmailType = 2,
Attachment = stationMaxReportName, RecipientEmail = user.Email,
RecipientName = user.Name, Subject = "綜合報表",
Type = 1 Attachment = stationMaxReportName,
}; RecipientName = user.Name,
List<string> properties = new List<string>() Type = 1
};
List<string> properties = new List<string>()
{ {
"UserId", "UserId",
"EmailType", "EmailType",
@ -304,7 +310,8 @@ namespace SolarPower.Quartz.Jobs
"RecipientName", "RecipientName",
"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,27 +324,30 @@ 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));
NoticeSchedule MaxmonthSchedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
RecipientEmail = user.Email, NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
Subject = "綜合報表", {
Attachment = stationReportmaxmonthName, RecipientEmail = user.Email,
RecipientName = user.Name, Subject = "綜合報表",
Type = 1, Attachment = stationReportmaxmonthName,
UserId = user.Id, RecipientName = user.Name,
EmailType = 2 Type = 1,
}; UserId = user.Id,
List<string> properties2 = new List<string>() EmailType = 2
{ };
"UserId", List<string> properties2 = new List<string>()
"EmailType", {
"RecipientEmail", "UserId",
"Subject", "EmailType",
"Attachment", "RecipientEmail",
"RecipientName", "Subject",
"Type" "Attachment",
}; "RecipientName",
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2); "Type"
};
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
}
} }
} }
@ -360,27 +370,30 @@ 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 != "")
{ {
NoticeSchedule MonthSchedule = new NoticeSchedule() if (IsValidEmail(user.Email))
{ {
RecipientEmail = user.Email, NoticeSchedule MonthSchedule = new NoticeSchedule()
Subject = "月報表", {
Attachment = stationReportmonthName, RecipientEmail = user.Email,
RecipientName = user.Name, Subject = "月報表",
Type = 1, Attachment = stationReportmonthName,
UserId = user.Id, RecipientName = user.Name,
EmailType = 1 Type = 1,
}; UserId = user.Id,
List<string> properties2 = new List<string>() EmailType = 1
{ };
"UserId", List<string> properties2 = new List<string>()
"EmailType", {
"RecipientEmail", "UserId",
"Subject", "EmailType",
"Attachment", "RecipientEmail",
"RecipientName", "Subject",
"Type" "Attachment",
}; "RecipientName",
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2); "Type"
};
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,46 +44,48 @@ namespace SolarPower.Quartz.Jobs
var updateNoticeSchedules = new List<NoticeSchedule>(); var updateNoticeSchedules = new List<NoticeSchedule>();
foreach (var notice in noticeSchedules) foreach (var notice in noticeSchedules)
{ {
var attachments = new List<string>(); if (IsValidEmail(notice.RecipientEmail)) {
var recipientEmails = new List<string>() var attachments = new List<string>();
{ var recipientEmails = new List<string>()
notice.RecipientEmail {
}; notice.RecipientEmail
if (!string.IsNullOrEmpty(notice.Attachment)) };
{ if (!string.IsNullOrEmpty(notice.Attachment))
attachments = notice.Attachment.Split(',').ToList(); {
attachments = notice.Attachment.Split(',').ToList();
}
var result = Send(recipientEmails, notice.Subject, notice.Content, attachments);
if (result.CompareTo("成功") == 0)
{
NoticeSchedule noticeSchedule = new NoticeSchedule();
noticeSchedule.Id = notice.Id;
noticeSchedule.IsDelivery = 1;
noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
updateNoticeSchedules.Add(noticeSchedule);
}
else
{
NoticeSchedule noticeSchedule = new NoticeSchedule();
noticeSchedule.Id = notice.Id;
noticeSchedule.IsDelivery = 2;
noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
noticeSchedule.Reason = result;
updateNoticeSchedules.Add(noticeSchedule);
}
List<string> properties = new List<string>()
{
"Id",
"IsDelivery",
"DeliveryAt"
};
await noticeScheduleRepository.UpdateList(updateNoticeSchedules, properties);
} }
var result = Send(recipientEmails, notice.Subject, notice.Content, attachments);
if (result.CompareTo("成功") == 0)
{
NoticeSchedule noticeSchedule = new NoticeSchedule();
noticeSchedule.Id = notice.Id;
noticeSchedule.IsDelivery = 1;
noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
updateNoticeSchedules.Add(noticeSchedule);
}
else
{
NoticeSchedule noticeSchedule = new NoticeSchedule();
noticeSchedule.Id = notice.Id;
noticeSchedule.IsDelivery = 2;
noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
noticeSchedule.Reason = result;
updateNoticeSchedules.Add(noticeSchedule);
}
List<string> properties = new List<string>()
{
"Id",
"IsDelivery",
"DeliveryAt"
};
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
{ {
@ -648,15 +649,20 @@ 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)
{ {
value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id}, {emailType})"); if (IsValidEmail(a.Email))
{
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)
{ {
@ -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})(\]?)$");
}
} }
} }