如何解决在网络应用程序上注册帐户后使用 sendgrid 发送电子邮件的问题
我创建了一个 asp.net 核心 Web 应用程序 (mvc) 项目,我在其中集成了 nuget 包 SendGrid
。出于某种原因,每当我注册帐户并点击提交时,它都会告诉我
因此,当我查看电子邮件时,它没有显示,甚至垃圾文件夹也没有显示。 From = new EmailAddress()
,我使用了一个不存在且不起作用的电子邮件。因此,我决定使用我在创建 sendgrid 帐户时使用的电子邮件来获取 api 密钥,但这也不起作用,即使在创建全新的电子邮件之后也是如此。
是否我正在做的事情导致电子邮件没有发送到我在帐户注册中列出的电子邮件?
在 Startup.cs
内部,我为 sendgrid 添加了瞬态:
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
我的班级 EmailSenderService.cs
处理电子邮件发送:
public class EmailSenderService : IEmailSender
{
public EmailSenderService(IOptions<AuthMessageSenderOptions> optionsAccessor)
{
Options = optionsAccessor.Value;
}
public AuthMessageSenderOptions Options { get; }
public Task SendEmailAsync(string email,string subject,string message)
{
return Execute(Options.SendGridKey,subject,message,email);
}
public Task Execute(string apiKey,string message,string email)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("info@example.com","Identity Demo"),Subject = subject,PlainTextContent = message,HtmlContent = message
};
msg.AddTo(new EmailAddress(email));
// disable click tracking.
// See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
msg.SetClickTracking(false,false);
return client.SendEmailAsync(msg);
}
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=.\\sqlExpress;Database=aspnet-ASP.NET_Core_IdentityDemo-2BDAC3A8-E5A9-492C-891F-EE1996D434EE;Trusted_Connection=True;MultipleActiveResultSets=true"
},"Logging": {
"LogLevel": {
"Default": "information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "information"
}
},"SendGridUser": "apikey","SendGridKey": "MY API KEY","AllowedHosts": "*"
}
我检查了 API 的设置是否允许发送电子邮件,它是...
更新
Ctor 将 Options
设置为值时出错。
解决方法
我想建议一些对我有用的更改。
首先,我将修改您的电子邮件发件人类,如下所示:
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
public class EmailSender : IEmailSender
{
public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
{
Options = optionsAccessor.Value;
}
public AuthMessageSenderOptions Options { get; }
public Task SendEmailAsync(string email,string subject,string message)
{
return Execute(Options.SendGridKey,subject,message,email);
}
public Task Execute(string apiKey,string message,string email)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("info@example.com","Identity Demo"),Subject = subject,PlainTextContent = message,HtmlContent = message
};
msg.AddTo(new EmailAddress(email));
// Disable click tracking.
// See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
msg.SetClickTracking(false,false);
return client.SendEmailAsync(msg);
}
}
为您的项目创建一个名为 AuthMessageSenderOptions 的新类:
public class AuthMessageSenderOptions
{
public string SendGridUser { get; set; }
public string SendGridKey { get; set; }
}
更新 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddTransient<IEmailSender,EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
}
最后,更新您的 appSettings.json 文件:
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQLExpress;Database=aspnet-ASP.NET_Core_IdentityDemo-2BDAC3A8-E5A9-492C-891F-EE1996D434EE;Trusted_Connection=True;MultipleActiveResultSets=true"
},"Logging": {
"LogLevel": {
"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"
}
},"SendGridUser": "apikey","SendGridKey": "INSERT YOUR API KEY","AllowedHosts": "*"
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。