使用mvc 5

时间:2016-10-17 12:26:14

标签: c# asp.net-mvc

我不知道我做错了什么,但我希望在用户注册并收到电子邮件后。我一直在关注msdn教程和其他教程,我尝试了不同的应用程序。

帐户控制器

public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        user.Email = model.Email;
        user.Confirmed = false;
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
                new System.Net.Mail.MailAddress("21408704@dut4life.ac.za", "Registration"),
                new System.Net.Mail.MailAddress(user.Email));
            m.Subject = "Confirmation of Email";
            m.Body = string.Format("Dear {0} <br/>Thank you please click below: <ahref=\"{1}\" title=\"user Email Confirm\">{1} <\a>", user.UserName, Url.Action
                ("Confirmed Email", "Accounts", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
            m.IsBodyHtml = true;
            m.BodyEncoding = System.Text.Encoding.UTF8;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "pod51014.outlook.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("21408704@dut4life.ac.za", "Dut951121");

            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;

            try
            {
                smtp.Send(m);
            }
            catch (Exception e)
            {
                throw e;
            }

            return RedirectToAction("Confirmed Email", "Accounts", new { Email = user.Email });
        }
        AddErrors(result);
    }

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

注册模型

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

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

我在加载很长时间后出现此错误

    Server Error in '/' Application.
    Source Error: 
    Line 180:                    catch (Exception e)
    Line 181:                    {
    Line 182:                        throw e;
    Line 183:                    }
    Line 184:
    The operation has timed out. 


   Exception Details: System.Net.Mail.SmtpException: The operation has timed    out.

1 个答案:

答案 0 :(得分:1)

问题出在您的SMTP客户端上。我认为您必须在UseDefaultCredentials之前设置Credentials,如下所示:

SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("21408704@dut4life.ac.za", "Dut951121");

如果它仍然不适合你,请告诉我。

<强>更新

一个简单的SMTP客户端如下所示:

using System.Net.Mail;

...

MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredentials("user","pass");
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);