You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.5 KiB

  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using Swift.DAL.BL.Remit.Transaction;
  5. using Swift.DAL.BL.System.UserManagement;
  6. using Swift.DAL.SwiftDAL;
  7. using Swift.web.Library;
  8. namespace Swift.web.SwiftSystem.Notification.LoginLogs
  9. {
  10. public partial class ResetPassword : System.Web.UI.Page
  11. {
  12. private const string ViewFunctionId = "10101170";
  13. private readonly ApplicationUserDao _obj = new ApplicationUserDao();
  14. private readonly RemittanceLibrary _sl = new RemittanceLibrary();
  15. private SmtpMailSetting smtpMailSetting = new SmtpMailSetting();
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. PopulateUserName();
  19. if (!IsPostBack)
  20. {
  21. Authenticate();
  22. }
  23. }
  24. protected string GetAgent()
  25. {
  26. return GetStatic.ReadQueryString("agentId", "");
  27. }
  28. protected void btnReset_Click(object sender, EventArgs e)
  29. {
  30. Update();
  31. }
  32. private void Authenticate()
  33. {
  34. _sl.CheckAuthentication(ViewFunctionId);
  35. btnReset.Visible = _sl.HasRight(ViewFunctionId);
  36. }
  37. protected string GetUserName()
  38. {
  39. return GetStatic.ReadQueryString("userName", "");
  40. }
  41. protected string GetuserId()
  42. {
  43. return GetStatic.ReadQueryString("userId", "");
  44. }
  45. protected void PopulateUserName()
  46. {
  47. userName.Text = GetUserName();
  48. userName.Enabled = false;
  49. }
  50. private void Update()
  51. {
  52. DbResult dbResult = _obj.ResetPassword(GetStatic.GetUser(), GetuserId());
  53. ManageMessage(dbResult);
  54. }
  55. private void ManageMessage(DbResult dbResult)
  56. {
  57. GetStatic.SetMessage(dbResult);
  58. if (dbResult.ErrorCode == "0")
  59. {
  60. SetupEmailSetting();
  61. SendMail();
  62. Response.Redirect("List.aspx");
  63. }
  64. else
  65. {
  66. GetStatic.PrintMessage(Page);
  67. }
  68. }
  69. private delegate void DoStuff(); //delegate for the action
  70. private void SendMail()
  71. {
  72. var myAction = new DoStuff(AsyncMailProcessing);
  73. //invoke it asynchrnously, control passes to next statement
  74. myAction.BeginInvoke(null, null);
  75. }
  76. private void AsyncMailProcessing()
  77. {
  78. var bw = new BackgroundWorker();
  79. // this allows our worker to report progress during work
  80. bw.WorkerReportsProgress = true;
  81. // what to do in the background thread
  82. bw.DoWork += new DoWorkEventHandler(
  83. delegate(object o, DoWorkEventArgs args)
  84. {
  85. var b = o as BackgroundWorker;
  86. smtpMailSetting.SendSmtpMail(smtpMailSetting);
  87. });
  88. // what to do when progress changed (update the progress bar for example)
  89. bw.ProgressChanged += new ProgressChangedEventHandler(
  90. delegate(object o, ProgressChangedEventArgs args)
  91. {
  92. //label1.Text = string.Format("{0}% Completed", args.ProgressPercentage);
  93. });
  94. // what to do when worker completes its task (notify the user)
  95. bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
  96. delegate(object o, RunWorkerCompletedEventArgs args)
  97. {
  98. var dbResult = new DbResult();
  99. dbResult.SetError("0", "Mail Sent Successfully", "");
  100. GetStatic.PrintMessage(Page, dbResult);
  101. });
  102. bw.RunWorkerAsync();
  103. }
  104. private void SetupEmailSetting()
  105. {
  106. var obj = new TranViewDao();
  107. var ds = obj.GetEmailFormat(GetStatic.GetUser(), "PwdReset", userName.Text, "", "");
  108. if (ds == null)
  109. return;
  110. if (ds.Tables.Count == 0)
  111. return;
  112. if (ds.Tables.Count > 1)
  113. {
  114. //Email Server Settings
  115. if (ds.Tables[0].Rows.Count > 0)
  116. {
  117. var dr1 = ds.Tables[0].Rows[0];
  118. smtpMailSetting.SmtpServer = dr1["smtpServer"].ToString();
  119. smtpMailSetting.SmtpPort = Convert.ToInt32(dr1["smtpPort"]);
  120. smtpMailSetting.SendEmailId = dr1["sendID"].ToString();
  121. smtpMailSetting.SendEmailPwd = dr1["sendPSW"].ToString();
  122. }
  123. if (ds.Tables[1].Rows.Count == 0)
  124. return;
  125. //Email Receiver
  126. if (ds.Tables[1].Rows.Count > 0)
  127. {
  128. var dt = ds.Tables[1];
  129. foreach (DataRow dr2 in dt.Rows)
  130. {
  131. if (!string.IsNullOrEmpty(smtpMailSetting.ToEmails))
  132. smtpMailSetting.ToEmails = smtpMailSetting.ToEmails + ",";
  133. smtpMailSetting.ToEmails = smtpMailSetting.ToEmails + dr2["email"].ToString();
  134. }
  135. }
  136. //Email Subject and Body
  137. if (ds.Tables[2].Rows.Count > 0)
  138. {
  139. var dr3 = ds.Tables[2].Rows[0];
  140. if (dr3 == null)
  141. return;
  142. smtpMailSetting.MsgSubject = dr3[0].ToString();
  143. smtpMailSetting.MsgBody = dr3[1].ToString();
  144. }
  145. }
  146. }
  147. }
  148. }