【发布时间】:2020-07-04 10:43:53
【问题描述】:
嘿, 我开始使用 IMailservice 后一直收到此错误
开发模式 切换到开发环境会显示有关所发生错误的详细信息。 不应为已部署的应用程序启用开发环境。它可能导致向最终用户显示来自异常的敏感信息。对于本地调试,通过将 ASPNETCORE_ENVIRONMENT 环境变量设置为 Development 并重新启动应用程序来启用开发环境。
我的申请尚未发布。
我在 launchSettings.json 中的环境变量设置为 Development,
我还使用>setx从命令行设置它,
我还检查了项目>属性>调试。
我的邮箱服务
{
Task SendEmailAsync(string toEmail, string subject, string content);
}
public class SendGridMailService : IMailService
{
public async Task SendEmailAsync(string toEmail, string subject, string content)
{
var apiKey = ConfigurationManager.AppSettings["SendGrid"];
var client = new SendGridClient(apiKey);
var from = new EmailAddress("somemail@gmail.com", "MyApplication");
var to = new EmailAddress(toEmail);
var msg = MailHelper.CreateSingleEmail(from, to, subject, content, content);
var response = await client.SendEmailAsync(msg);
}
}
这是我的注册操作
[HttpPost]
public async Task<IActionResult> Registers(Registration registration)
{
if (ModelState.IsValid)
{
var user = new IdentityUser
{
UserName = registration.Email,
Email = registration.Email
};
var result = await userManager.CreateAsync(user, registration.Password);
if (result.Succeeded)
{
var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action("ConfirmEmail", "Accounts", new { userId = user.Id, token }, Request.Scheme);
await mailService.SendEmailAsync(registration.Email, "Account Confirmation", confirmationLink);
ViewBag.ErrorTitle = "Registration Successful";
ViewBag.ErrorMessage = "Before you can login, Please Confirm your email. Thank you";
return RedirectToPage("/Error");
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
return View(registration);
}
我还将启动类更改为
if (env.IsDevelopment() || env.IsProduction() || env.IsStaging())
{
app.UseDeveloperExceptionPage();
}
【问题讨论】:
标签: c# asp.net-core