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.Threading.Tasks;
using System.Transactions;
using System.Text.RegularExpressions;
namespace SolarPower.Controllers
{
@ -676,30 +677,36 @@ namespace SolarPower.Controllers
_ => ""
};
var user = await userRepository.GetOneAsync(person);
NoticeSchedule schedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
EmailType = 3,
Type = 1,
UserId = person,
RecipientName = user.Name,
RecipientEmail = user.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
NoticeSchedule schedule = new NoticeSchedule()
{
EmailType = 3,
Type = 1,
UserId = person,
RecipientName = user.Name,
RecipientEmail = user.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
}
}
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
}
}
if (post.FixFirm != 0)
@ -714,27 +721,31 @@ namespace SolarPower.Controllers
};
//var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId);
var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm");
NoticeSchedule schedule = new NoticeSchedule()
if (IsValidEmail(firm.Email))
{
EmailType = 3,
Type = 1,
RecipientName = firm.Name,
RecipientEmail = firm.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
NoticeSchedule schedule = new NoticeSchedule()
{
EmailType = 3,
Type = 1,
RecipientName = firm.Name,
RecipientEmail = firm.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
}
}
}
//using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress))
@ -898,30 +909,36 @@ namespace SolarPower.Controllers
_ => ""
};
var user = await userRepository.GetOneAsync(person);
NoticeSchedule schedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
EmailType = 3,
Type = 1,
UserId = person,
RecipientName = user.Name,
RecipientEmail = user.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
NoticeSchedule schedule = new NoticeSchedule()
{
EmailType = 3,
Type = 1,
UserId = person,
RecipientName = user.Name,
RecipientEmail = user.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
noticeSchedules.Add(schedule);
}
}
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
if (noticeSchedules.Count > 0)
{
await noticeScheduleRepository.AddAnyThing<List<NoticeSchedule>>(noticeSchedules, properties, "notice_schedule");
}
}
if (post.FixFirm != 0)
{
@ -935,29 +952,31 @@ namespace SolarPower.Controllers
};
// var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId);
var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync<OperationInfo>(post.FixFirm, powerstations.SiteDB, "operation_firm");
NoticeSchedule schedule = new NoticeSchedule()
if (IsValidEmail(firm.Email))
{
EmailType = 3,
Type = 1,
RecipientName = firm.Name,
RecipientEmail = firm.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddAnyThing<NoticeSchedule>(schedule, properties, "notice_schedule");
NoticeSchedule schedule = new NoticeSchedule()
{
EmailType = 3,
Type = 1,
RecipientName = firm.Name,
RecipientEmail = firm.Email,
Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime,
Content = "維修項目:" + post.FixDo + "</br>注意事項 :" + post.Notice,
};
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.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Quartz.Jobs
{
@ -249,27 +250,30 @@ namespace SolarPower.Quartz.Jobs
var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented));
if (stationReportName != "")
{
NoticeSchedule DaySchedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
UserId = user.Id,
EmailType = 0,
RecipientEmail = user.Email,
Subject = "日報表",
Attachment = stationReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
NoticeSchedule DaySchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 0,
RecipientEmail = user.Email,
Subject = "日報表",
Attachment = stationReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
}
}
//綜合報表 每日
@ -284,17 +288,19 @@ namespace SolarPower.Quartz.Jobs
Userid = user.Id
};
var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented));
NoticeSchedule MaxSchedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
UserId = user.Id,
EmailType = 2,
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationMaxReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
NoticeSchedule MaxSchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 2,
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationMaxReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
@ -304,7 +310,8 @@ namespace SolarPower.Quartz.Jobs
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
}
if (DateTime.Now.ToString("dd") == "01")
{
@ -317,27 +324,30 @@ namespace SolarPower.Quartz.Jobs
Userid = user.Id
};
var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented));
NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationReportmaxmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 2
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationReportmaxmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 2
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
}
}
}
@ -360,27 +370,30 @@ namespace SolarPower.Quartz.Jobs
var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
if (stationReportmonthName != "")
{
NoticeSchedule MonthSchedule = new NoticeSchedule()
if (IsValidEmail(user.Email))
{
RecipientEmail = user.Email,
Subject = "月報表",
Attachment = stationReportmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 1
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
NoticeSchedule MonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "月報表",
Attachment = stationReportmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 1
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
}
}
}
@ -437,5 +450,11 @@ namespace SolarPower.Quartz.Jobs
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.Net.Mail;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Quartz.Jobs
{
@ -43,46 +44,48 @@ namespace SolarPower.Quartz.Jobs
var updateNoticeSchedules = new List<NoticeSchedule>();
foreach (var notice in noticeSchedules)
{
var attachments = new List<string>();
var recipientEmails = new List<string>()
{
notice.RecipientEmail
};
if (!string.IsNullOrEmpty(notice.Attachment))
{
attachments = notice.Attachment.Split(',').ToList();
if (IsValidEmail(notice.RecipientEmail)) {
var attachments = new List<string>();
var recipientEmails = new List<string>()
{
notice.RecipientEmail
};
if (!string.IsNullOrEmpty(notice.Attachment))
{
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】【任務完成】");
@ -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.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace SolarPower.Repository.Implement
{
@ -648,15 +649,20 @@ namespace SolarPower.Repository.Implement
List<string> value = new List<string>();
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());
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)
{
@ -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})(\]?)$");
}
}
}