public async Task sendMail(string Id) { var user = await UserManager.Users.Where(x => x.UserName.Contains("S") && x.UserName == Id).SingleOrDefaultAsync(); //sa table user table //重設資料庫該 user 密碼 並 hash 存入 db //重設db密碼 //1.重設 user 密碼 string pwd = generateFirstPwd(); await UserManager.UpdateSecurityStampAsync(user.Id); user.PasswordHash = UserManager.PasswordHasher.HashPassword(pwd); user.LastPasswordChangedDate = null; var SupAcc = db.SupplierAccount.Where(x => x.SupplierAccountID == Id).SingleOrDefault(); SupAcc.PasswordHash = user.PasswordHash; // 傳送包含此連結的電子郵件 var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Views\Shared\ResetPwdSupEmailTemplate.html")); // 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; string SupAccIDstr = user.UserName; string MailBody = MembersDBService.getMailBody(tempMail, img, callbackUrl, pwd, SupAccIDstr); //寄信 await UserManager.SendEmailAsync(user.Id, "重設您的密碼", MailBody); //3.更新db寄信相關欄位 //SendLetterDate SupAcc.SendLetterDate = DateTime.Now; //SendLetterStatus SupAcc.SendLetterStatus = "S"; await updateTable(user, SupAcc); //新增Supplier var userRoles = await UserManager.GetRolesAsync(user.Id); if (!userRoles.Contains("Supplier")) { user.Roles.Clear(); var result = await UserManager.AddToRolesAsync(user.Id, "Supplier"); } //更新狀態欄位 user sa table await AccStatusReset(Id); }
//SendEmail public async Task SendMailToBuyer(List <OrderDtlForMail> shipNotice) { /////////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); string supplierAccount = supplier.SupplierAccountID; string supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// string supAccID = supplierAccount; //string borderColor = "border-color:black"; //string borderLine = "1"; //string shipDtlMail = $"<h2>出貨單號:{shipNotice.FirstOrDefault().ShipNoticeID}</h2>"; //shipDtlMail += $"<table style={borderColor} border={borderLine}><thead><tr><th>出貨商品明細流水號</th><th>料件編號</th><th>料件名稱</th><th>出貨數量</th><th>出貨日期</th></tr><thead><tbody>"; //foreach (var snd in shipNotice) //{ // DateTime shipDate = (DateTime)snd.ShipDate; // shipDtlMail += "<tr>"; // shipDtlMail += $"<td>{snd.ShipNoticeOID}</td><td>{snd.PartNumber}</td><td>{snd.PartName}</td><td>{snd.ShipQty}</td><td>{shipDate.ToString("yyyy/MM/dd")}</td>"; // shipDtlMail += "</tr>"; //} //shipDtlMail += "</tbody></table>"; string snId = shipNotice[0].ShipNoticeID; PurchaseOrder order = db.PurchaseOrder.Find(db.ShipNotice.Find(snId).PurchaseOrderID); string OrderID = order.PurchaseOrderID; string OrderApply = "已出貨"; string borderSize = "1"; // borderColor = "border:'1'"; //borderColor += "sans-serif;margin: 20px auto; background-color: #eee; padding-bottom: 20px;"; string OrderDtl = $"<tr><td colspan='5'><h2>出貨明細</h2></br><h2>出貨單號:{snId}</h2></td></tr>"; OrderDtl += "<tr><td>出貨明細編號</td><td>料件編號</td><td>料件名稱</td><td>出貨數量</td><td>出貨日期</td></tr>"; foreach (var snd in shipNotice) { DateTime shipDate = (DateTime)snd.ShipDate; OrderDtl += $"<tr>"; OrderDtl += $"<td>{snd.ShipNoticeOID}</td><td>{snd.PartNumber}</td><td>{snd.PartName}</td><td>{snd.ShipQty}</td><td>{shipDate.ToString("yyyy/MM/dd")}</td>"; OrderDtl += "</tr>"; } string SupplierName = db.SupplierInfo.Where(x => x.SupplierCode == supplierCode).SingleOrDefault().SupplierName; string BuyerID = db.Employee.Where(x => x.EmployeeID == order.EmployeeID).SingleOrDefault().EmployeeID; string emp = db.PurchaseOrder.Find(db.ShipNotice.Find(snId).PurchaseOrderID).EmployeeID; string EmployeeName = db.Employee.Find(emp).Name; var user = UserManager.Users.Where(x => x.UserName == emp).SingleOrDefault(); var userId = user.Id; string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Areas\SupplierArea\Views\Shared\SendMailToBuyer.html")); string MailBody = MembersDBService.getMailBody(tempMail, OrderID, OrderApply, EmployeeName, OrderDtl, SupplierName); //寄信 await UserManager.SendEmailAsync(userId, "商品出貨通知", MailBody); // bool a = UserManager.SendEmailAsync(userId, "商品出貨通知", shipDtlMail).IsCompleted; }
//寄送出貨通知明細給公司採購 public async Task sendShipDtlMailToBuyer(List <int> shipDtlList) { string tempMail = ""; string img = ""; string callbackUrl = ""; string pwd = ""; string MailBody = MembersDBService.getMailBody(tempMail, img, callbackUrl, pwd); //寄信 //await UserManager.SendEmailAsync(user.Id, "重設您的密碼", MailBody); //var user = await UserManager.Users.Where(x => x.UserName.Contains("C") && x.UserName == EmpId).SingleOrDefaultAsync(); //// 傳送包含此連結的電子郵件 //var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Views\Shared\ResetPwdEmailTemplate.html")); //// 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 //string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; //string pwd = generateFirstPwd(); //string MailBody = MembersDBService.getMailBody(tempMail, img, callbackUrl, pwd); ////emp table user table ////重設資料庫該 user 密碼 並 hash 存入 db ////重設db密碼 ////1.重設 user 密碼 //await UserManager.UpdateSecurityStampAsync(user.Id); //user.PasswordHash = UserManager.PasswordHasher.HashPassword(pwd); //user.LastPasswordChangedDate = null; //var emp = db.Employee.Where(x => x.EmployeeID == EmpId).SingleOrDefault(); //emp.PasswordHash = user.PasswordHash; ////寄信 //await UserManager.SendEmailAsync(user.Id, "重設您的密碼", MailBody); ////3.更新db寄信相關欄位 ////SendLetterDate //emp.SendLetterDate = DateTime.Now; ////SendLetterStatus //emp.SendLetterStatus = "S"; ////更新狀態欄位 user emo table //await AccStatusReset(EmpId); }
//寄信 public async Task SendMailToBuyer(PurchaseOrder order, string Reply, string OrderDtl) { /////////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); string supplierAccount = supplier.SupplierAccountID; string supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// //string borderColor = "border-color:black"; //string borderLine = "1"; //string BuyerName = db.Employee.Find(order.EmployeeID).Name; //string shipDtlMail = $"<table style='{borderColor}' border='{borderLine}'>"; //shipDtlMail += $"<thead><tr><th>{BuyerName},你好</th></tr></thead>"; //shipDtlMail += $"<tr><td>訂單編號:{order.PurchaseOrderID}{Reply}</td></tr>"; //shipDtlMail += "</table>"; string OrderID = order.PurchaseOrderID; string OrderApply = Reply; if (OrderDtl == null) { OrderDtl = ""; } string SupplierName = db.SupplierInfo.Where(x => x.SupplierCode == supplierCode).SingleOrDefault().SupplierName; string BuyerID = db.Employee.Where(x => x.EmployeeID == order.EmployeeID).SingleOrDefault().EmployeeID; string EmployeeName = db.Employee.Find(BuyerID).Name; var user = UserManager.Users.Where(x => x.UserName == BuyerID).SingleOrDefault(); var userId = user.Id; // 如需如何進行帳戶確認及密碼重設的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkID=320771 // 傳送包含此連結的電子郵件 //var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); //更改密碼要在code之前不然他是拿UpdateSecurityStampAsync 來生code的 //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Areas\SupplierArea\Views\Shared\SendMailToBuyer.html")); // 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 //string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; string MailBody = MembersDBService.getMailBody(tempMail, OrderID, OrderApply, EmployeeName, OrderDtl, SupplierName); //寄信 await UserManager.SendEmailAsync(userId, "供應商訂單答交通知", MailBody); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByEmailAsync(model.Email); //if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) if (user == null) { // 不顯示使用者不存在或未受確認 return(View("ForgotPasswordConfirmation")); } //// 如需如何進行帳戶確認及密碼重設的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkID=320771 //// 傳送包含此連結的電子郵件 //string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); //var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "重設密碼", "請按 <a href=\"" + callbackUrl + "\">這裏</a> 重設密碼"); //return RedirectToAction("ForgotPasswordConfirmation", "Account"); //判斷登入者為供應方、採購方 string LoginAccId = user.UserName; string LognId = user.Id; //採購方 if (UserManager.IsInRole(LognId, "Buyer") || UserManager.IsInRole(LognId, "Manager") || UserManager.IsInRole(LognId, "ProductionControl") || UserManager.IsInRole(LognId, "NewEmployee") || UserManager.IsInRole(LognId, "Admin") || UserManager.IsInRole(LognId, "Warehouse")) { //採購方 忘記密碼重設密碼 //重設db密碼 //1.重設 user 密碼 string pwd = generateFirstPwd(); await UserManager.UpdateSecurityStampAsync(LognId); user.PasswordHash = UserManager.PasswordHasher.HashPassword(pwd); user.LastPasswordChangedDate = null; await UserManager.UpdateAsync(user); var emp = db.Employee.Where(x => x.EmployeeID == LoginAccId).SingleOrDefault(); emp.PasswordHash = user.PasswordHash; // 傳送包含此連結的電子郵件 var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); string codeB = await UserManager.GenerateEmailConfirmationTokenAsync(LognId); var callbackUrlB = Url.Action("ConfirmEmail", "Account", new { userId = LognId, code = codeB }, protocol: Request.Url.Scheme); string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Views\Shared\ResetPwdEmailTemplate.html")); // 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; string MailBody = MembersDBService.getMailBody(tempMail, img, callbackUrlB, pwd); //寄信 await UserManager.SendEmailAsync(LognId, "重設您的密碼", MailBody); //3.更新db寄信相關欄位 //SendLetterDate emp.SendLetterDate = DateTime.Now; //SendLetterStatus emp.SendLetterStatus = "S"; //更新狀態欄位 user emo table await AccStatusResetEmp(LoginAccId); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } //供應方 else if (UserManager.IsInRole(LognId, "Supplier")) { //供應商 忘記密碼重設密碼//sa table user table //重設資料庫該 user 密碼 並 hash 存入 db //重設db密碼 //1.重設 user 密碼 string pwd = generateFirstPwd(); await UserManager.UpdateSecurityStampAsync(LognId); user.PasswordHash = UserManager.PasswordHasher.HashPassword(pwd); user.LastPasswordChangedDate = null; var SupAcc = db.SupplierAccount.Where(x => x.SupplierAccountID == LoginAccId).SingleOrDefault(); SupAcc.PasswordHash = user.PasswordHash; // 傳送包含此連結的電子郵件 var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); string code = await UserManager.GenerateEmailConfirmationTokenAsync(LognId); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = LognId, code = code }, protocol: Request.Url.Scheme); string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Views\Shared\ResetPwdSupEmailTemplate.html")); // 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; string SupAccIDstr = user.UserName; string MailBody = MembersDBService.getMailBody(tempMail, img, callbackUrl, pwd, SupAccIDstr); //寄信 await UserManager.SendEmailAsync(LognId, "重設您的密碼", MailBody); //3.更新db寄信相關欄位 //SendLetterDate SupAcc.SendLetterDate = DateTime.Now; //SendLetterStatus SupAcc.SendLetterStatus = "S"; await updateTable(user, SupAcc); //新增Supplier var userRoles = await UserManager.GetRolesAsync(LognId); if (!userRoles.Contains("Supplier")) { user.Roles.Clear(); var result = await UserManager.AddToRolesAsync(LognId, "Supplier"); } //更新狀態欄位 user sa table await AccStatusResetSup(LoginAccId); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } } // 如果執行到這裡,發生某項失敗,則重新顯示表單 return(View(model)); }