电子邮件不在 mvc 中发送,但在 asp.net webform 中有效

email doesnt send in mvc but works in asp.net webform

我最近切换到 ASP.NET MVC。我想在人们注册我的网站时发送电子邮件确认。

所以我取消注释 ASP.NET MVC 默认为此设置的代码,并在 web.config 中添加配置,但这不起作用,我一直有这个错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

我创建了一个 asp.net 网络表单并尝试从该项目发送电子邮件并且成功了。因此,我复制了我在页面加载中用于在网络表单中发送电子邮件的代码,并将其放入帐户控制器中的注册操作中,但我再次遇到该错误。

我真的不明白为什么我在 ASP.NET MVC 中会出现这个错误,但完全相同的代码在 webforms 中工作正常。

这是 ASP.NET MVC 注册操作中的代码:

public async Task<ActionResult> Register(RegisterViewModel model)
{
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, BirthDate=model.BirthDate };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // 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.GenerateEmailConfirmationTokenAsync(user.Id);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //create the mail message 
                MailMessage mail = new MailMessage();

                //set the addresses 
                mail.From = new MailAddress("info@wwwebco.com"); //IMPORTANT: This must be same as your smtp authentication address.
                mail.To.Add(user.Id);

                //set the content 
                mail.Subject = "This is an email";
                mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
                //send the message 
                SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

                //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
                NetworkCredential Credentials = new NetworkCredential("info@wwwebco.com", "MyPassWord");
                smtp.Credentials = Credentials;
                await smtp.SendMailAsync(mail);

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

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

我在 asp.net webforms 应用程序中的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    //create the mail message 
    MailMessage mail = new MailMessage();

    //set the addresses 
    mail.From = new MailAddress("info@wwwebco.com"); //IMPORTANT: This must be same as your smtp authentication address.
    mail.To.Add("armanhafezi@gmail.com");

    //set the content 
    mail.Subject = "This is an email";
    mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
    //send the message 
    SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

    //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
    NetworkCredential Credentials = new NetworkCredential("info@wwwebco.com", "MyPassWord");
    smtp.Credentials = Credentials;
    smtp.Send(mail); 

}

我真的被这个问题困住了。请帮忙!

谢谢。

更新:

你为什么使用 user.Id 而不是 model.Email

如果您有任何空白或遗漏的详细信息,这会导致错误吗?

原文:

您应该将 mail.From 更改为 mail.Sender

您是否将 ASP.NET Web 窗体和 MVC 加载到同一主机环境? 是否可以阻止端口 25,或者要求通过 HTTPS 发送 SMTP?

对于 HTTPS,您需要更改端口号,通常是端口 465,但可能会有所不同,具体取决于您的邮件发件人。

如果您的 SMTP 服务器有限制,请尝试使用 SendGrid,它有一个免费帐户。