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
+12 -1
View File
@@ -5,6 +5,7 @@ using Application.Endpoints.Doctors.Registration;
using Application.Endpoints.Doctors.ResetPassword;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Application.Services.Email;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
@@ -20,14 +21,16 @@ public class DoctorsController : ControllerBase
private readonly IDoctorRepository _database;
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IJwtService _jwtService;
private readonly IEmailService _emailService;
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService, IAppointmentsMongoDbService appointmentsMongoDbService)
IJwtService jwtService, IAppointmentsMongoDbService appointmentsMongoDbService, IEmailService emailService)
{
_database = database;
_hashingAlgorithms = hashingAlgorithms;
_jwtService = jwtService;
_appointmentsMongoDbService = appointmentsMongoDbService;
_emailService = emailService;
}
[HttpPost("register")]
@@ -35,6 +38,14 @@ public class DoctorsController : ControllerBase
{
var handler = new DoctorRegistrationHandler(_database, _hashingAlgorithms);
var response = await handler.Handle(doctorRegistrationDto).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
var body = _emailService.GenerateCredentialsEmailBody(
doctorRegistrationDto.Email, doctorRegistrationDto.Password);
await _emailService.SendEmailAsync(doctorRegistrationDto.Email, "Successful registration!", body);
}
return StatusCode(response.StatusCode, response);
}
+13 -1
View File
@@ -5,7 +5,9 @@ using Application.Endpoints.Patients.Registration;
using Application.Endpoints.Patients.ResetPassword;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Email;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -18,14 +20,16 @@ public class PatientsController : ControllerBase
private readonly IJwtService _jwtService;
private readonly IMedicalHistoryRepository _medicalHistory;
private readonly IPatientRepository _patientRepository;
private readonly IEmailService _emailService;
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService, IMedicalHistoryRepository medicalHistory)
IJwtService jwtService, IMedicalHistoryRepository medicalHistory, IEmailService emailService)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
_jwtService = jwtService;
_medicalHistory = medicalHistory;
_emailService = emailService;
}
[HttpGet]
@@ -97,6 +101,14 @@ public class PatientsController : ControllerBase
{
var handler = new PatientRegistrationHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.Handle(patientRegistrationDto).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
var body = _emailService.GenerateCredentialsEmailBody(
patientRegistrationDto.Email, patientRegistrationDto.Password);
await _emailService.SendEmailAsync(patientRegistrationDto.Email, "Successful registration!", body);
}
return StatusCode(response.StatusCode, response);
}
+8
View File
@@ -24,6 +24,14 @@
"Audience": "HealthCareManagerUsers",
"ExpirationTime": 1440
},
"SmtpSettings": {
"Host": "smtp-relay.brevo.com",
"Port": 587,
"From": "bot@healthcaremanager.com",
"EnableSSL": false,
"UserName": "andreimihneacerbu@gmail.com",
"Password": "zpK3w71LkNa0sGt6"
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -24,6 +24,14 @@
"Audience": "HealthCareManagerUsers",
"ExpirationTime": 1440
},
"SmtpSettings": {
"Host": "smtp-relay.brevo.com",
"Port": 587,
"From": "bot@healthcaremanager.com",
"EnableSSL": false,
"UserName": "andreimihneacerbu@gmail.com",
"Password": "zpK3w71LkNa0sGt6"
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("API")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+015a1a9c09fe601a8c8b9f61de5c7d87b5a73607")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0d03c0ea4308c032d708d3ee63f648b10811c65f")]
[assembly: System.Reflection.AssemblyProductAttribute("API")]
[assembly: System.Reflection.AssemblyTitleAttribute("API")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
fe88730eccbbece1fca0539b2a9e626db000fc875172a108baa980cc6e2addff
9da6326faa16b70419b8bad27961193604ad75fcbf0f437bde7752d1858a93b2
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/015a1a9c09fe601a8c8b9f61de5c7d87b5a73607/*"}}
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/0d03c0ea4308c032d708d3ee63f648b10811c65f/*"}}
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,7 @@
namespace Application.Services.Email;
public interface IEmailService
{
Task SendEmailAsync(string to, string subject, string body);
string GenerateCredentialsEmailBody(string email, string password);
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Application")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+015a1a9c09fe601a8c8b9f61de5c7d87b5a73607")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0d03c0ea4308c032d708d3ee63f648b10811c65f")]
[assembly: System.Reflection.AssemblyProductAttribute("Application")]
[assembly: System.Reflection.AssemblyTitleAttribute("Application")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
0cb94e40182d1b6cd5700fb1c27327d72d0012508a1d486af3eaad106de53053
5ebc7e7564dc4079f778b09cac511dae388a84af6e87589df0498d0a9bc6364f
@@ -1 +1 @@
d60e6c6b63d7c5372a4c397784287b9f857dcb3972545a2d619ebddba564fc35
26e7a7899234aa69b1e72639220f61d98aa121dd4eb93d18c3595c34f36f0d99
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/015a1a9c09fe601a8c8b9f61de5c7d87b5a73607/*"}}
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/0d03c0ea4308c032d708d3ee63f648b10811c65f/*"}}
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Core")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+015a1a9c09fe601a8c8b9f61de5c7d87b5a73607")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0d03c0ea4308c032d708d3ee63f648b10811c65f")]
[assembly: System.Reflection.AssemblyProductAttribute("Core")]
[assembly: System.Reflection.AssemblyTitleAttribute("Core")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
67e59afccb449e467919aa45b8e4747b643eec04fa68b13d647704ff737ee4fc
5a6e74a12f0945636560f529431fe24f2f54ca0d381dda4f2ec4349776fcc2bd
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/015a1a9c09fe601a8c8b9f61de5c7d87b5a73607/*"}}
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/0d03c0ea4308c032d708d3ee63f648b10811c65f/*"}}
Binary file not shown.
Binary file not shown.
+4 -1
View File
@@ -1,8 +1,10 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Application.Services.Email;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Infrastructure.Data;
using Infrastructure.Services.Email;
using Infrastructure.Services.HashingAlgorithms;
using Infrastructure.Services.MongoDB;
using Infrastructure.Services.PostgreSQL;
@@ -65,7 +67,8 @@ public static class DependencyInjection
return new JwtService(configuration);
});
// services.AddScoped<IEmailService, EmailService>();
services.Configure<SmtpSettings>(configuration.GetSection("SmtpSettings"));
services.AddScoped<IEmailService, EmailService>();
return services;
}
@@ -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);
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace Infrastructure;
public class SmtpSettings
{
public string Host { get; set; }
public int Port { get; set; }
public string From { get; set; }
public bool EnableSSL { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Infrastructure")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+015a1a9c09fe601a8c8b9f61de5c7d87b5a73607")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0d03c0ea4308c032d708d3ee63f648b10811c65f")]
[assembly: System.Reflection.AssemblyProductAttribute("Infrastructure")]
[assembly: System.Reflection.AssemblyTitleAttribute("Infrastructure")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
2df5e0f93be8613fe06b8383df37707a7c1dcd41012222f47d70221d4c2a316f
216a0769924e0848d0c09eb46e477765dd2a8d7e28503106a43024c1a9d666a1
@@ -1 +1 @@
f7db5be1a68464cc50c7ccee2ec8ca8c6b9870991fcbe7daa2045fdfe78ab58e
36246167bc67f799ef7d06b38addbdbc2934572b797d99ee0e80ef15f6df67cd
@@ -1 +1 @@
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/015a1a9c09fe601a8c8b9f61de5c7d87b5a73607/*"}}
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/0d03c0ea4308c032d708d3ee63f648b10811c65f/*"}}