我在这里缺少什么?我在mvcapp中发送电子邮件

时间:2015-06-24 12:36:49

标签: c# email asp.net-mvc-5

我试图在用户忘记密码时向mvc应用发送电子邮件,这是我的code.pls帮助

这是我的businesslogic中的电子邮件类

     public class EmailBusiness 
{

    public bool SendEmailForgot(string email, string url)
    {
        bool isvalid = false;
        try
        {

            var boddy = new StringBuilder();

            boddy.Append("Hi! " + email + "<br/>");
            boddy.Append("Click  the link to reset your " + url);

            string bodyFor = boddy.ToString();
            string toFor = email;
            const string subjectFor = "Reset Password";

            WebMail.SmtpServer = "pod51014.outlook.com";
            WebMail.SmtpPort = 587;
            WebMail.UserName = "20822526@dut4life.ac.za";
            WebMail.Password = "Password";
            WebMail.From = "20822526@dut4life.ac.za";
            WebMail.EnableSsl = true;

            WebMail.Send(to: toFor, subject: subjectFor, body: bodyFor);

            isvalid = true;
            return isvalid;
        }
        catch
        {
            return isvalid;
        }
    }

这是我的Accountviewmodels中的忘记密码模型

public class ForgotPasswordViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }
}

这是我在accountcontroller中的忘记密码操作

     [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model, string sms)
    {
        EmailBusiness _emailBusiness = new EmailBusiness();
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.Email);
            if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
                // Don't reveal that the user does not exist or is not confirmed
                return View("ForgotPasswordConfirmation");
            }

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
             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, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
             _emailBusiness.SendEmailForgot(model.Email, callbackUrl);
             return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

这是我的观点

 @model Template.Model.ForgotPasswordViewModel
    @{
 ViewBag.Title = "Forgot your password?";
 Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
 }

 <h2>@ViewBag.Title.</h2>

  @using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post,  new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Enter your email.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Email Link" />
    </div>
</div>

}

这在我的webconfig中

 <appSettings>
<add key="from" value="20822526@dut4life.ac.za" />
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

0 个答案:

没有答案