Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不使用ASP.NET Identity Framework发送电子邮件_C#_Asp.net_Email_Asp.net Identity - Fatal编程技术网

C# 不使用ASP.NET Identity Framework发送电子邮件

C# 不使用ASP.NET Identity Framework发送电子邮件,c#,asp.net,email,asp.net-identity,C#,Asp.net,Email,Asp.net Identity,我使用此代码发送电子邮件以重置用户密码: // // POST: /Account/ForgotPassword [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel model, bool CaptchaVali

我使用此代码发送电子邮件以重置用户密码:

//
        // POST: /Account/ForgotPassword
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel model, bool CaptchaValid)
        {
            string mensaje = String.Empty;

            if (ModelState.IsValid)
            {
                if (!CaptchaValid)
                    mensaje = "ERROR: Captcha inválido.";
                else
                {
                    var user = await UserManager.FindByEmailAsync(model.Email);
                    if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                    {
                        mensaje = "ERROR: La dirección de correo electrónico no está registrada.";
                    }
                    else
                    {
                        var provider = new DpapiDataProtectionProvider("WebAttendance");
                        UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(provider.Create("UserToken")) as IUserTokenProvider<ApplicationUser, string>;
                        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", "Por favor, cambie su contraseña al hacer click <a href=\"" + callbackUrl + "\">aquí</a>");
                        return Json("INFO: Se envió un mail a su cuenta de correo con instrucciones para cambiar su contraseña.");
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return Json(mensaje);
        }
//
//POST:/帐户/放弃密码
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务ForgotPassword(ForgotPasswordViewModel模型,bool CaptchaValid)
{
string mensaje=string.Empty;
if(ModelState.IsValid)
{
如果(!CaptchaValid)
mensaje=“错误:验证码inválido。”;
其他的
{
var user=await UserManager.findbyemailsync(model.Email);
if(user==null | |!(等待UserManager.IsEmailConfirmedAsync(user.Id)))
{
mensaje=“错误:电力公司不需要注册。”;
}
其他的
{
var provider=新的DpapiDataProtectionProvider(“网络考勤”);
UserManager.UserTokenProvider=新的DataProtectorTokenProvider”);
返回Json(“信息:SEEnvióun mail a su cuenta de correo con instrucions para cambian su contraseña.”;
}
}
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回Json(mensaje);
}
上面的代码运行时没有任何错误,但实际上并没有发送电子邮件

这是Web.config条目:

  <system.net>
    <mailSettings>
      <smtp from="info@xxx.com">
        <network host="mail.desytec.cl" password="xxxx" port="587" userName="yyy@xxx.cl"  enableSsl="false"/>
      </smtp>
    </mailSettings>
  </system.net>


顺便说一句,当我阅读一些帖子时,我知道我必须在App_Start中有IdentityConfig.cs文件,但该文件丢失了。这可能是原因吗?

你可能已经得到了答案,但这是谷歌给我看的第一篇帖子,所以为了完整性,我回答了

查看
App_Start
文件夹中的
IdentityConfig.cs
文件。您应该可以找到类似以下代码:

public class EmailService : IIdentityMessageService {
    public Task SendAsync(IdentityMessage message) {
        // Plug in your email service here to send an email.
        return Task.FromResult(0);
    }
}
我误解了其他几篇文章,认为我必须用上面的代码创建一个新类。相反,你需要更新此方法以使用任何你想使用的电子邮件服务发送电子邮件

我最终得到了类似的结果,但与此不完全相同,因此您必须测试这是否真的适用于您

// code from https://stackoverflow.com/questions/40674457/how-to-configure-sender-email-credentials-for-asp-net-identity-usermanager-sende
public async Task SendAsync(IdentityMessage message) {
    // Plug in your email service here to send an email.
    SmtpClient client = new SmtpClient();
    await client.SendMailAsync("email from web.config here",
                                message.Destination,
                                message.Subject,
                                message.Body);
}

您在代码中的何处发送电子邮件?您在何处初始化smtp并使用smtp配置发送电子邮件?我认为这是由UserManager.SendEmailAsync自动完成的call@YuriS我以前读过。看看我写的关于IdentityConfig.cs的内容。不过,我用那篇文章中显示的代码手动添加了该文件,但它确实添加了不起作用。如果您没有该文件,则意味着您没有正确创建该项目。仅添加它是不够的。还有一些代码设置。例如,在公共静态ApplicationUserManager创建(IdentityFactoryOptions,IOwinContext上下文)中,应调用var manager=new ApplicationUserManager(new UserStore(context.Get());然后manager.EmailService=new-EmailService();我会正确地重新创建项目