瀏覽代碼

修正邮件发送了的bug(使用第三方组件MailKit)

luocaiyang 3 年之前
父節點
當前提交
88c4c8d864

+ 1 - 0
wispro.sp.api/Controllers/AccountController.cs

@@ -124,6 +124,7 @@ namespace wispro.sp.api.Controllers
 
         }
 
+        
         [HttpGet, HttpPost, Route("GetRoles")]
         public List<string> GetRoles(string ResourceId)
         {

+ 2 - 0
wispro.sp.api/Controllers/PerformanceItemController.cs

@@ -308,6 +308,7 @@ namespace wispro.sp.api.Controllers
         /// <returns></returns>
         public ListApiResponse<PerformanceItem> GetMyList(int userid, int type,int pageIndex=1,int pageSize = 10)
         {
+            
             ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
             var results = Context.PerformanceItems
                 .Where<PerformanceItem>(s =>
@@ -686,6 +687,7 @@ namespace wispro.sp.api.Controllers
                             item.CaseMemo = pointRecord.Reason;
                             item.DoItem = doItem.DoItem;
                             item.CaseCoefficient = doItem.DoItemCoefficient;
+                            item.Type = "专案";
                             item.CalMonthId = calMonth.Id;
                             Context.PerformanceItems.Add(item);
                             Context.SaveChanges();

+ 4 - 22
wispro.sp.api/Job/ImportReportJob.cs

@@ -94,29 +94,15 @@ namespace wispro.sp.api.Job
 
             }
 
-
             //每月绩效统计--发客户超过一个月未完成案件
             DownloadReport( "每月绩效统计--发客户超过一个月未完成案件", calMonth,false);
-            //NewMethod("506aa7ad-c3f4-4ec6-9ec8-ff6b92dcd7c1", "每月绩效统计--发客户超过一个月未完成案件.xlsx", calMonth);
-
+            
             //每月绩效统计--上个月递交完成案件
             DownloadReport("每月绩效统计--上个月递交完成案件", calMonth, true);
-            //NewMethod("d7308cd2-71e4-4444-9f47-f4d731ddb26a", "每月绩效统计--上个月递交完成案件.xlsx", calMonth);
-
+            
             //每月绩效统计--中国一次OA授权表
             DownloadReport("每月绩效统计--中国一次OA授权表", calMonth, true,true);
-            //NewMethod("72454834-afdd-4b98-b42a-0bc912d07610", "每月绩效统计--中国一次OA授权表.xlsx", calMonth,true);
-
-
-            var Staffs = spDb.Staffs.Join(spDb.ItemStaffs.Where<ItemStaff>(p=>p.Item.CalMonthId == calMonth.Id), 
-                                        staff => staff.Id, 
-                                        istaff => istaff.DoPersonId, 
-                                        (staff, istaff) => new Staff
-                                        {
-                                            Id = staff.Id,
-                                            Name = staff.Name,
-                                            Mail = staff.Mail
-                                        });
+            
              var lstStaff =   spDb.Staffs.Where<Staff>(s=>s.ItemStaffs.Where<ItemStaff>(p=>p.Item.CalMonthId == calMonth.Id).Count<ItemStaff>()>0);
                 
 
@@ -126,9 +112,7 @@ namespace wispro.sp.api.Job
                 string strBody = $"<html><body>{staff.Name},您好!<br/></br/>&nbsp;&nbsp;&nbsp;&nbsp;<div>{calMonth.Year}年{calMonth.Month}月绩效数据已生成,请在{DateTime.Now.AddDays(4).ToString("yyyy年MM月dd日")}之前确认完成!</div></body></html>";
                 string strTo = "luocaiyang@wispro-china.com";
 
-                QuartzUtil.AddMailJob(strSubject,strBody,strTo);
-
-                break;
+                _ = QuartzUtil.AddMailJob(strSubject, strBody, staff.Name, strTo);
             }
            
             return Task.CompletedTask;
@@ -232,8 +216,6 @@ namespace wispro.sp.api.Job
             return Task.CompletedTask;
         }
 
-        
-
         private PerformanceItem Row2Item_1(DataRow row,  CalMonth calMonth)
         {
             PerformanceItem item = new PerformanceItem();

+ 3 - 1
wispro.sp.api/Job/MailJob.cs

@@ -10,10 +10,12 @@ namespace wispro.sp.api.Job
     {
         public Task Execute(IJobExecutionContext context)
         {
+            
             string Subject = context.JobDetail.JobDataMap.Get("Subject").ToString();
             string Body = context.JobDetail.JobDataMap.Get("Body").ToString();
             string To = context.JobDetail.JobDataMap.Get("To").ToString();
-            utility.MailUtil.SendEmail(Subject,Body,To);
+            string Reciver = context.JobDetail.JobDataMap.Get("Reciever").ToString();
+            utility.MailUtil.SendEmail(Subject,Body,Reciver,To);
             return Task.CompletedTask;
         }
     }

+ 2 - 2
wispro.sp.api/Job/QuartzUtil.cs

@@ -42,11 +42,10 @@ namespace wispro.sp.api.Job
             await _scheduler.ScheduleJob(job, trigger);
         }
 
-        public static async Task AddMailJob(string Subject,string Body,string To)
+        public static async Task AddMailJob(string Subject,string Body,string toName,string To)
         {
             await Init();
 
-
             var trigger = TriggerBuilder.Create()
                .WithDescription("发送邮件通知")
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(0))
@@ -59,6 +58,7 @@ namespace wispro.sp.api.Job
             job.JobDataMap.Put("Subject",Subject);
             job.JobDataMap.Put("Body", Body);
             job.JobDataMap.Put("To", To);
+            job.JobDataMap.Put("Reciever", toName);
 
             await _scheduler.ScheduleJob(job, trigger);
         }

+ 19 - 28
wispro.sp.utility/MailUtil.cs

@@ -2,44 +2,35 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
-using System.Net.Mail;
+
 using System.Text;
 using System.Threading.Tasks;
+using MailKit.Net.Smtp;
+using MimeKit;
 
 namespace wispro.sp.utility
 {
     public class MailUtil
     {
-        public static void SendEmail(string subject, string body, string toEmail)
+        public static void SendEmail(string subject, string body,string toMailName, string toEmail)
         {
-            
-            // 命令行参数必须是SMTP主机
-            SmtpClient client = new SmtpClient(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")));
+            MimeMessage message = new MimeMessage();
+            MailboxAddress from = new MailboxAddress("绩效系统",ConfigHelper.GetSectionValue("MailSetting:mail"));
+            message.From.Add(from);
+            MailboxAddress to = new MailboxAddress(toMailName, toEmail);
+            message.To.Add(to);
+            message.Subject = subject;
 
-            string User = ConfigHelper.GetSectionValue("MailSetting:Account");
-            string PassWord = ConfigHelper.GetSectionValue("MailSetting:Password"); // 服务平台获取
-            client.UseDefaultCredentials = false;
-            client.EnableSsl = true;
-            client.Credentials = new System.Net.NetworkCredential(User, PassWord);
-            // 发送人
-            MailAddress from = new MailAddress(ConfigHelper.GetSectionValue("MailSetting:mail"), "绩效系统", Encoding.UTF8);
-            // 接收人
-            MailAddress to = new MailAddress(toEmail);
+            BodyBuilder bodyBuilder = new BodyBuilder();
+            bodyBuilder.HtmlBody = body;
+            message.Body = bodyBuilder.ToMessageBody();
 
-            // 指定邮件内容
-            MailMessage message = new MailMessage(from, to);
-            message.Body = body;
-            message.BodyEncoding = Encoding.UTF8;
-            message.IsBodyHtml = true;
-            // 主题
-            message.Subject = subject;
-            message.SubjectEncoding = Encoding.UTF8;
-            // 设置发送操作结束时回调的方法.
-            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
-            string userState = subject;
-            client.SendAsync(message,null);
-            //Console.WriteLine("发送消息...");
-           
+            SmtpClient client = new SmtpClient();
+            client.Connect(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")),true);  //例如:smtp.exmail.qq.com,465
+            client.Authenticate(ConfigHelper.GetSectionValue("MailSetting:Account"), ConfigHelper.GetSectionValue("MailSetting:Password")); //发送邮件的账户密码
+            client.Send(message);
+            client.Disconnect(true);
+            client.Dispose();
         }
 
         private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)

+ 1 - 0
wispro.sp.utility/wispro.sp.utility.csproj

@@ -10,6 +10,7 @@
   </ItemGroup>
 
   <ItemGroup>
+    <PackageReference Include="MailKit" Version="2.15.0" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.10" />
     <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
     <PackageReference Include="NPOI" Version="2.5.4" />

+ 24 - 23
wispro.sp.winClient/Form1.cs

@@ -173,37 +173,38 @@ namespace wispro.sp.winClient
 
         private async void button3_Click(object sender, EventArgs e)
         {
-            CreateAppealModel model = new CreateAppealModel();
-            HttpClient http = new HttpClient();
-            PerformanceItem item = await http.GetFromJsonAsync<wispro.sp.entity.PerformanceItem>($"http://localhost:39476/api/PerformanceItem/Get?Id=7341");
-            List<AppealType> appealTypes = await http.GetFromJsonAsync<List<AppealType>>($"http://localhost:39476/api/Appeal/GetAppealTypes");
-            var appealType = appealTypes.Where<AppealType>(a => a.Id == 1).FirstOrDefault();
-            await model.Init(item, appealType);
-            //await TestQueryFilter();
-            List<PerformanceItem> retList = new List<PerformanceItem>();
-            var test= retList.Where<PerformanceItem>(p => p.isDanger());
+            wispro.sp.utility.MailUtil.SendEmail("测试邮件标题","测试邮件内容","罗才洋","luocaiyang@139.com");
+            //CreateAppealModel model = new CreateAppealModel();
+            //HttpClient http = new HttpClient();
+            //PerformanceItem item = await http.GetFromJsonAsync<wispro.sp.entity.PerformanceItem>($"http://localhost:39476/api/PerformanceItem/Get?Id=7341");
+            //List<AppealType> appealTypes = await http.GetFromJsonAsync<List<AppealType>>($"http://localhost:39476/api/Appeal/GetAppealTypes");
+            //var appealType = appealTypes.Where<AppealType>(a => a.Id == 1).FirstOrDefault();
+            //await model.Init(item, appealType);
+            ////await TestQueryFilter();
+            //List<PerformanceItem> retList = new List<PerformanceItem>();
+            //var test= retList.Where<PerformanceItem>(p => p.isDanger());
 
-            await InitRules(true);
+            //await InitRules(true);
 
-            //return;
+            ////return;
 
-            await ImportUsers();
-            await InputPerformanceItem("ExcelFiles\\21.01-21.06 工程师绩效报表-总表.xlsx", true, false, 0);
+            //await ImportUsers();
+            //await InputPerformanceItem("ExcelFiles\\21.01-21.06 工程师绩效报表-总表.xlsx", true, false, 0);
 
-            //CalMonth cal = new CalMonth()
-            //{
-            //    Year = 2021,
-            //    Month = 9,
-            //    Status = 0
-            //};
+            ////CalMonth cal = new CalMonth()
+            ////{
+            ////    Year = 2021,
+            ////    Month = 9,
+            ////    Status = 0
+            ////};
 
-            //await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--发客户超过一个月未完成案件.xlsx", true,false,1, cal);
+            ////await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--发客户超过一个月未完成案件.xlsx", true,false,1, cal);
 
-            //await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--上个月递交完成案件.xlsx", true, false, 1, cal);
+            ////await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--上个月递交完成案件.xlsx", true, false, 1, cal);
 
-            //await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--中国一次OA授权表.xlsx", true, false, 1, cal,true);
+            ////await InputPerformanceItem(@"C:\Users\luowen\Downloads\每月绩效统计--中国一次OA授权表.xlsx", true, false, 1, cal,true);
 
-            MessageBox.Show("导入完成!");
+            //MessageBox.Show("导入完成!");
         }
 
         private PerformanceItem Row2Item_1(DataRow row, List<Staff> Staffs, CalMonth calMonth)

+ 8 - 0
wispro.sp.winClient/appsettings.json

@@ -8,5 +8,13 @@
     "ScheduleSetting": "30 15 17 * * ? *",
     "IPEasyWeb": "http://47.106.221.167/Login.aspx"
 
+  },
+  "MailSetting": {
+    "Server": "smtp.exmail.qq.com",
+    "Port": "465",
+    "Account": "luocaiyang@china-wispro.com",
+    "Password": "Lqftiu807005",
+    "mail": "luocaiyang@china-wispro.com"
+
   }
 }

+ 20 - 0
wospro.sp.entity/Authorize/ResourceAuthority.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace wispro.sp.entity
+{
+    /// <summary>
+    /// 资源权限
+    /// </summary>
+    public class ResourceAuthority
+    {
+        public Guid Id { get; set; }
+
+        public string Name { get; set; }
+
+        public string Roles { get; set; }
+    }
+}

wospro.sp.entity/UserDefine.cs → wospro.sp.entity/Authorize/UserDefine.cs