从Asp.net Web API电子邮件确认中的Gmail收件箱无法使用Callbackurl

时间:2019-02-24 17:11:50

标签: asp.net-web-api

在Web API中完成用户注册后,我生成了一个回调URL。网址已在我的Gmail框中成功发送。但是,当我单击链接时,它会打开一个新标签,并且加载圆圈不断旋转。它没有打我的后端方法。但是,当我从邮递员调用具有相同网址的方法时,它会命中,如果我右键单击链接并在新选项卡中按打开链接,它也会命中。这是我的Register方法的代码。

[HttpPost]
    [AllowAnonymous]
    [Route("api/Register")]
    public async Task<IHttpActionResult> Register(User model)
    {
        if (model == null)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser()
        {
            UserName = model.UserName,
            Email = model.Email,
            PhoneNumber = model.PhoneNumber
        };
        user.FirstName = model.FirstName;
        user.LastName = model.LastName;
        user.CompanyId = model.CompanyId;
        user.CompanyName = model.CompanyName;

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

        // var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
        var callbackUrl = 
            Url.Link("DefaultApi", new { controller = "Account", action = "ConfirmEmail", userId = user.Id, code = code });

        //await UserManager
        //    .SendEmailAsync(user.Id, "Confirm your account",
        //        "Please confirm your account by clicking <a href=\"" 
        //         + callbackUrl + "\">here</a>");

        await UserManager.
            SendEmailAsync(user.Id, 
                "Confirm your account", "Please confirm your account by clicking <a href=\""
                                        + callbackUrl + "\">here</a>");



        return Ok(result);

    }

电子邮件服务:

public class EmailService: IIdentityMessageService
{
    public async Task SendAsync(IdentityMessage message)
    {
        await configSendGridasync(message);
    }
    private async Task configSendGridasync(IdentityMessage message)
    {

        dynamic MailMessage = new MailMessage();
        MailMessage.From = new MailAddress("myemail@gmail.com");
        MailMessage.To.Add(message.Destination);
        MailMessage.Subject = message.Subject;
        MailMessage.IsBodyHtml = true;
        MailMessage.Body = message.Body;

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
        smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");

        try
        {
            try
            {
                smtpClient.Send(MailMessage);

            }
            catch (Exception ex)
            {

            }
        }
        catch (SmtpFailedRecipientsException ex)
        {
            for (int i = 0; i <= ex.InnerExceptions.Length; i++)
            {
                SmtpStatusCode status = ex.StatusCode;
                if ((status == SmtpStatusCode.MailboxBusy) | (status == SmtpStatusCode.MailboxUnavailable))
                {
                    System.Threading.Thread.Sleep(5000);
                    smtpClient.Send(MailMessage);
                }
            }
        }

    }

有什么办法解决这个问题?

0 个答案:

没有答案