0

嗨,我在 asp.net 中创建了一个需要用户身份验证的应用程序。我已激活“电子邮件确认”选项。这适用于本地数据库,但一旦在天蓝色上,就不起作用。电子邮件确认代码如下:

public Task SendAsync(IdentityMessage message)
        {
            
            return Task.Factory.StartNew(() =>
            {
            sendMail(message);
        });

    }

    
    void sendMail(IdentityMessage message)
    {
        #region formatter
       
        string html =  message.Body;
      

      
        #endregion

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
        msg.To.Add(new MailAddress(message.Destination));
        msg.Subject = message.Subject;
      
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

        SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", Convert.ToInt32(587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
        smtpClient.Credentials = credentials;
        smtpClient.EnableSsl = true;
        smtpClient.Send(msg);
    }

}

我尝试了sendgrid,但它似乎不起作用,所以我使用了outlook。

这是注册部分的代码:

   public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {                   
            string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");                 

                ViewBag.Message = "Check your email and confirm your account. You must confirm your email " + "before you can log in.";

                return View("Info");

                              }
            else 
            { return View("Error"); }

            AddErrors(result);
        }

最后是最后一部分的代码

private async Task<string> SendEmailConfirmationTokenAsync(string userID, string subject)
        {
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(userID);
            var callbackUrl = Url.Action("ConfirmEmail", "Account",
               new { userId = userID, code = code }, protocol: Request.Url.Scheme);
            await UserManager.SendEmailAsync(userID, subject,
               "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            return callbackUrl;
        }
4

2 回答 2

0

当您的代码在 Azure 中运行时,它可能无法访问 Outlook,这可以解释为什么没有发送电子邮件。

SendGrid 是从 Azure 功能、Web 作业或任何应用程序发送电子邮件的推荐方法。这里有一个关于如何使用 SendGrid 的很好的解释:https ://blog.mailtrap.io/azure-send-email/#What_do_I_need_to_send_emails_from_Azure

于 2020-07-14T21:06:18.920 回答
0

终于让它工作了。实际上,当我尝试它时它以前有效,但确认进入了我的垃圾邮件文件夹......Doh。

    public async Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.

         var apiKey = ConfigurationManager.AppSettings["SendGridAPI"];
                   var client = new SendGridClient(apiKey);
        var from = new EmailAddress("your email address", "your name");
        var subject = message.Subject;
        var to = new EmailAddress(message.Destination);
        var plainTextContent = message.Body;
        var htmlContent = message.Body;
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = await client.SendEmailAsync(msg);


    }
于 2020-07-25T11:46:05.527 回答