示例#1
0
        public async Task <OTPResponse> SendOTP(SendOTPRequest oData)
        {
            OTPResponse sResponse = new OTPResponse();

            try
            {
                var users = await _usersData.FindByUsernameAsync(oData.userName);

                if (users.Count > 0)
                {
                    var    email        = users[0].email;
                    var    mobile       = users[0].mobileNo;
                    var    userFullName = users[0].name;
                    var    otp          = GenerateOTPNumber();
                    var    host         = _config.GetSection("SMTPDetails").GetSection("host").Value;
                    var    port         = _config.GetSection("SMTPDetails").GetSection("port").Value;
                    var    uName        = _config.GetSection("SMTPDetails").GetSection("username").Value;
                    string pwd          = _config.GetSection("SMTPDetails").GetSection("pwd").Value;
                    string from         = _config.GetSection("SMTPDetails").GetSection("from").Value;
                    string cc           = _config.GetSection("ForgotPasswordSMTP").GetSection("recipients").Value;
                    string recipients   = email + cc;
                    string subject      = _config.GetSection("ForgotPasswordSMTP").GetSection("subject").Value;
                    string body         = _config.GetSection("ForgotPasswordSMTP").GetSection("message").Value + otp.ToString();

                    string mailTemplateSubject = _config.GetSection("ForgotPasswordMailTemplate").GetSection("Subject").Value;
                    string mailTemplateBody    = _config.GetSection("ForgotPasswordMailTemplate").GetSection("Body").Value;
                    string mailBody            = "";
                    //var forgetPasswordModel = JsonConvert.DeserializeObject<ForgetPassword>(mailTemplate);

                    if (email == "")
                    {
                        sResponse.status  = "false";
                        sResponse.message = "Email does not exist for this user";
                    }
                    else
                    {
                        mailBody = "";
                        mailBody = mailBody + mailTemplateBody.Replace("#OTP", otp).Replace("#RecipientName", userFullName);
                        var mailMessage = new MailMessage(from, recipients, mailTemplateSubject, mailBody);
                        mailMessage.IsBodyHtml = true;

                        var otpSend = _usersData.SendOTP(oData.userName, otp);
                        if (otpSend.msgRespone == true)
                        {
                            var client = new SmtpClient(host, int.Parse(port))
                            {
                                Credentials = new NetworkCredential(uName, pwd),
                                EnableSsl   = true
                            };
                            client.Send(mailMessage);
                            //client.Send(from, recipients, mailTemplateSubject, HttpUtility.HtmlDecode(mailBody));
                            sResponse.status  = "true";
                            sResponse.message = otpSend.msg;
                        }
                        else
                        {
                            sResponse.status  = "false";
                            sResponse.message = otpSend.msg;
                        }
                    }
                }
                else
                {
                    sResponse.status  = "false";
                    sResponse.message = "Username does not exist";
                }
                return(sResponse);
            }
            catch (Exception e)
            {
                sResponse.status  = "false";
                sResponse.message = e.Message;
            }
            return(sResponse);
        }