serviciu email done

This commit is contained in:
andrei-mihnea-cerbu
2024-04-09 03:03:44 +03:00
parent 0d03c0ea43
commit 9977a9b5f5
63 changed files with 135 additions and 17 deletions
@@ -0,0 +1,58 @@
using Application.Services.Email;
namespace Infrastructure.Services.Email;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
public class EmailService : IEmailService
{
private readonly SmtpSettings _smtpSettings;
public EmailService(IOptions<SmtpSettings> smtpSettings)
{
_smtpSettings = smtpSettings.Value;
}
public async Task SendEmailAsync(string to, string subject, string body)
{
Console.WriteLine(_smtpSettings.Host);
Console.WriteLine(_smtpSettings.Port);
using (var client = new SmtpClient(_smtpSettings.Host, _smtpSettings.Port))
{
client.EnableSsl = _smtpSettings.EnableSSL;
client.Credentials = new NetworkCredential(_smtpSettings.UserName, _smtpSettings.Password);
var mailMessage = new MailMessage
{
From = new MailAddress(_smtpSettings.From),
Subject = subject,
Body = body,
IsBodyHtml = true
};
mailMessage.To.Add(to);
await client.SendMailAsync(mailMessage);
}
}
public string GenerateCredentialsEmailBody(string email, string password)
{
string template = @"
<html>
<body>
<h1>Welcome to HealthcareManager</h1>
<p>You can now log in using the following credentials:</p>
<p><strong>Email:</strong> {email}</p>
<p><strong>Password:</strong> {password}</p>
<p>For security reasons, please change your password after logging in.</p>
</body>
</html>";
return template.Replace("{email}", email).Replace("{password}", password);
}
}