错误请求 检查“错误”以获得 API 返回的错误列表。在发送网格

Bad Request Check `Errors` for a list of errors returned by the API. at SendGrid

我正在使用发送网格在创建用户后发送电子邮件,我正在关注本教程 Here http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity 我什至下载了本教程并按原样编写了代码

但它的代码在此处中断

string code = manager.GenerateEmailConfirmationToken(uApp.Id);
string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, uApp.Id, Request);
 manager.SendEmail(uApp.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

这是第三个赞

manager.SendEmail(uApp.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

代码中断的地方

错误是这样的

Exceptions.InvalidApiRequestException: Bad Request Check Errors for a list of errors returned by the API. at SendGrid.Web.d__c.MoveNext() -- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at SendGrid.Web.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at GCRweb.EmailService.d__0.MoveNext()

bla bla bla 在此之前它在第一行给我一个关于一些令牌和东西的错误 我做错了什么

我试过那个,但我认为你能做的最好的就是为此创建你自己的函数这是一个合适的解决方案

public static bool ForgotPassword(string fromAccount, string toAccount, string subject, string msg)
    {
        var ConfirmationMail = new MailMessage();
        if (IsLiveMode)
        {
            ConfirmationMail = new MailMessage("abc@abc.com", toAccount, subject, msg);
        }
        else
        {
            ConfirmationMail = new MailMessage("abc@abc.com", toAccount, subject, msg);
                       }
         ConfirmationMail.Priority = MailPriority.High;
        ConfirmationMail.IsBodyHtml = true;
        SmtpClient objSMTPClient = new SmtpClient();
        try
        {
            objSMTPClient.Send(ConfirmationMail);
            return true;
        }
        catch
        {
            return false;
        }
    }

这是配置文件

<system.net>
<mailSettings>    
  <smtp from="no-reply@no-reply.com">
    <network host="smtp.live.com" password="abcdefghijk" port="587" userName="atteeqKhawaja@msn.com" enableSsl="true" />
  </smtp>     
</mailSettings>

最终代码现在是这样的

string code = manager.GeneratePasswordResetToken(user.Id);
            string callbackUrl = IdentityHelper.GetResetPasswordRedirectUrl(code, Request);

            bool emailSent = ForgotPassword(" khawaja Atteeq", Email.Text, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>.");

InvalidApiRequestException 异常是关键。

使用 DeliverAsync() 方法处理错误的最佳方式是这样的:

            try
            {
                await transportWeb.DeliverAsync(mensaje);
            }
            catch (InvalidApiRequestException ex)
            {
                var detalle = new StringBuilder();

                detalle.Append("ResponseStatusCode: " + ex.ResponseStatusCode + ".   ");
                for (int i = 0; i < ex.Errors.Count(); i++)
                {
                    detalle.Append(" -- Error #" + i.ToString() + " : " + ex.Errors[i]);
                }

                throw new ApplicationException(detalle.ToString(), ex);
            }

我遇到了同样的问题,检查Web.config。原来的教程说使用 value="[Sendgridname]",删除 [ ] 后它应该可以正常工作。见下文。

<appSettings>
    <add key="emailServiceUserName" value="SendGridUserName" />
    <add key="emailServicePassword" value="SendGridPassword" />
  </appSettings>