Backend login and register
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using Application.Interfaces;
|
||||
using Application.Models;
|
||||
using MailKit.Net.Smtp;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MimeKit;
|
||||
using MimeKit.Text;
|
||||
|
||||
namespace Application.Services;
|
||||
|
||||
public class EmailService(IConfiguration config, Func<ISmtpClient>? smtpClientFactory)
|
||||
: IEmailService
|
||||
{
|
||||
private readonly Func<ISmtpClient> _smtpClientFactory = smtpClientFactory ?? (() => new SmtpClient());
|
||||
|
||||
public EmailService(IConfiguration config) : this(config, null)
|
||||
{
|
||||
}
|
||||
|
||||
public void SendEmailAsync(EmailRequest emailRequest)
|
||||
{
|
||||
var emailMessage = new MimeMessage();
|
||||
var from = config["EmailSettings:From"];
|
||||
emailMessage.From.Add(new MailboxAddress("RssReader", from));
|
||||
emailMessage.To.Add(new MailboxAddress(emailRequest.To, emailRequest.To));
|
||||
emailMessage.Subject = emailRequest.Subject;
|
||||
emailMessage.Body = new TextPart(TextFormat.Html)
|
||||
{
|
||||
Text = string.Format(emailRequest.Content)
|
||||
};
|
||||
|
||||
var client = _smtpClientFactory();
|
||||
try
|
||||
{
|
||||
client.Connect(config["EmailSettings:SmtpServer"], 465, true);
|
||||
client.Authenticate(config["EmailSettings:From"], config["EmailSettings:Password"]);
|
||||
client.Send(emailMessage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.Disconnect(true);
|
||||
client.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user