要解决ASP.NET Core Identity的Yahoo电子邮件声明未收到的问题,您可以尝试以下代码示例:
Startup.cs
文件的ConfigureServices
方法中,添加以下代码以配置电子邮件服务:using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.DependencyInjection;
using System.Net;
using System.Net.Mail;
public void ConfigureServices(IServiceCollection services)
{
// 其他配置代码...
// 添加电子邮件服务
services.AddTransient();
// 其他配置代码...
}
EmailSender
的新类,并实现IEmailSender
接口。在该类中,添加以下代码来发送电子邮件:using Microsoft.AspNetCore.Identity.UI.Services;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
public class EmailSender : IEmailSender
{
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var fromAddress = new MailAddress("your-email@example.com", "Your Name");
var toAddress = new MailAddress(email);
const string fromPassword = "your-email-password";
string smptServer = "smtp.mail.yahoo.com";
int port = 587;
var smtp = new SmtpClient
{
Host = smptServer,
Port = port,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = htmlMessage,
IsBodyHtml = true
})
{
await smtp.SendMailAsync(message);
}
}
}
请确保将your-email@example.com
和your-email-password
替换为您自己的电子邮件地址和密码。
这样,当ASP.NET Core Identity发送电子邮件时,它将使用您配置的Yahoo电子邮件服务器来发送声明电子邮件。