0

こんにちは、ユーザー認証を必要とするasp.netでアプリを作成しました。「確認メール」オプションを有効にしました。これはローカル データベースでは正常に機能しましたが、Azure では機能しません。電子メール確認のコードは次のとおりです。

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

最後にそれが機能しました。実際に試してみたところ、以前は機能していましたが、確認がスパムフォルダーに送られました... 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 に答える