我有发送邮件控制器
这是接口代码
public interface IEmailService
{
Task SendEmail(string email, string subject, string message);
}
这是 API 控制器
[Route("api/send-email")]
[ApiController]
public class EmailController : ControllerBase
{
private readonly IEmailService _emailService;
public EmailController(IEmailService emailService)
{
_emailService = emailService;
}
[HttpPost]
public async Task<IActionResult> SendEmailAsync(string email, string subject, string message)
{
await _emailService.SendEmail(email, subject, message);
return Ok();
}
}
这是 SendEmail 类的代码
public class EmailService: IEmailService
{
private readonly IConfiguration _configuration;
public EmailService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task SendEmail(string email, string subject, string message)
{
using (var client = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = _configuration["Email:Email"],
Password = _configuration["Email:Password"]
};
client.Credentials = credential;
client.Host = _configuration["Email:Host"];
client.Port = int.Parse(_configuration["Email:Port"]);
client.EnableSsl = true;
using (var emailMessage = new MailMessage())
{
emailMessage.To.Add(new MailAddress(email));
emailMessage.From = new MailAddress(_configuration["Email:Email"]);
emailMessage.Subject = subject;
emailMessage.Body = message;
client.Send(emailMessage);
}
}
await Task.CompletedTask;
}
}
我尝试通过邮递员检查发布请求并将此 json 发送到此 url - https://localhost:5001/api/send-email
{ "email": "suhomlin.eugene93@gmail.com", "subject": "Get Medicine", "message": null }
但是我收到错误消息,该电子邮件为空。这是错误的屏幕
我不明白,为什么?我的错误在哪里?
叮当猫咪
杨魅力
相关分类