Files
FACULTATE-HEALTHCARE_MANAGER/backend/Infrastructure/Services/Email/EmailService.cs
T
2024-05-30 15:40:13 +03:00

97 lines
3.4 KiB
C#

using System.Net;
using System.Net.Mail;
using Application.Services.Email;
using Microsoft.Extensions.Options;
namespace Infrastructure.Services.Email;
public class EmailService(IOptions<SmtpSettings> smtpSettings) : IEmailService
{
private readonly SmtpSettings _smtpSettings = smtpSettings.Value;
public async Task SendEmailAsync(string to, string subject, string body)
{
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 name, string email, string password)
{
var 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>Thank you for choosing our services and we wish you an amazing day!</p>
</body>
</html>";
return template.Replace("{name}", name).Replace("{email}", email).Replace("{password}", password);
}
public string GenerateResetCredentialsEmailBody(string name, string email, string password)
{
var template = @"
<html>
<body>
<h1>Password Reset Successful</h1>
<p>Hello {name},
<p>Your password has been successfully reset. You can now log in to your HealthcareManager account using your new password.</p>
<p>If you did not request a password reset, please contact our support team immediately.</p>
<p>For security reasons, it's recommended to keep your password confidential and to change it regularly.</p>
</body>
</html>";
return template.Replace("{name}", name).Replace("{email}", email).Replace("{password}", password);
}
public string GenerateMessageNotificationEmail(string senderName, string receiverName, string chatUrl)
{
var template = @"
<html>
<body>
<h1>New Message Notification</h1>
<p>Hello {receiverName},</p>
<p>You have received a new message from {senderName}.</p>
<a href='{chatUrl}' target='_blank'>Click here to view the message</a>
<p>Thank you for using HealthcareManager!</p>
</body>
</html>";
return template.Replace("{senderName}", senderName)
.Replace("{receiverName}", receiverName)
.Replace("{chatUrl}", chatUrl);
}
public string GetSuccessfulRegistrationSubject()
{
return "Welcome to HealthcareManager!";
}
public string GetSuccessfulPasswordResetSubject()
{
return "Password reset successfully!";
}
public string GetMessageNotificationSubject()
{
return "New Message Notification!";
}
}