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
+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/*"}}