134 lines
4.9 KiB
C#
134 lines
4.9 KiB
C#
using MailKit.Net.Smtp;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Microsoft.Extensions.Options;
|
|
using MimeKit;
|
|
|
|
|
|
namespace MarcoBMS.Services.Service
|
|
{
|
|
public class EmailSender : IEmailSender
|
|
{
|
|
private readonly SmtpSettings _smtpSettings;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
public EmailSender(IOptions<SmtpSettings> emailSettings, IConfiguration configuration, IWebHostEnvironment env)
|
|
{
|
|
_smtpSettings = emailSettings.Value;
|
|
_configuration = configuration;
|
|
_env = env;
|
|
}
|
|
|
|
public async Task<string> GetEmailTemplate(string templateName, Dictionary<string, string> replacements)
|
|
{
|
|
string path = Path.Combine(_env.ContentRootPath, "EmailTemplates", $"{templateName}.html");
|
|
|
|
if (!File.Exists(path))
|
|
throw new FileNotFoundException("Template file not found");
|
|
|
|
string content = await File.ReadAllTextAsync(path);
|
|
|
|
foreach (var item in replacements)
|
|
{
|
|
content = content.Replace($"{{{{{item.Key}}}}}", item.Value);
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
public async Task SendResetPasswordEmailOnRegister(string toEmail, string toName, string resetLink)
|
|
{
|
|
var replacements = new Dictionary<string, string>
|
|
{
|
|
{ "MAIL_TITLE", "New registration,Reset Your Password" },
|
|
{ "RESET_PWD_URL", resetLink },
|
|
{ "RECEIVER_NAME", toName }
|
|
};
|
|
|
|
List<string> toEmails = new List<string>
|
|
{
|
|
toEmail
|
|
};
|
|
string emailBody = await GetEmailTemplate("new-user-email", replacements);
|
|
|
|
await SendEmailAsync(toEmails, "New user registration, Reset Your Password", emailBody);
|
|
}
|
|
public async Task SendResetPasswordEmail(string toEmail, string userName, string resetLink)
|
|
{
|
|
var replacements = new Dictionary<string, string>
|
|
{
|
|
{ "MAIL_TITLE", "Reset Your Password" },
|
|
{ "RESET_PWD_URL", resetLink }
|
|
};
|
|
|
|
string emailBody = await GetEmailTemplate("forgot-password", replacements);
|
|
|
|
List<string> toEmails = new List<string>
|
|
{
|
|
toEmail
|
|
};
|
|
|
|
await SendEmailAsync(toEmails, "Reset Your Password", emailBody);
|
|
|
|
}
|
|
|
|
public async Task SendResetPasswordSuccessEmail(string toEmail, string toName)
|
|
{
|
|
var replacements = new Dictionary<string, string>
|
|
{
|
|
{ "MAIL_TITLE", "Reset Password Successful" },
|
|
{ "RECEIVER_NAME", toName }
|
|
};
|
|
|
|
string emailBody = await GetEmailTemplate("password-change-success", replacements);
|
|
|
|
List<string> toEmails = new List<string>
|
|
{
|
|
toEmail
|
|
};
|
|
await SendEmailAsync(toEmails, "Password Changed Successfully", emailBody);
|
|
|
|
}
|
|
public async Task SendRequestDemoEmail(List<string> toEmails, InquiryEmailObject demoEmailObject)
|
|
{
|
|
|
|
var replacements = new Dictionary<string, string>
|
|
{
|
|
{ "MAIL_TITLE", "User Requested a Demo" },
|
|
{ "ORGANIZATION_NAME", demoEmailObject.OrganizatioinName ?? string.Empty },
|
|
{ "EMAIL", demoEmailObject.Email ?? string.Empty},
|
|
{ "ABOUT", demoEmailObject.About ?? string.Empty },
|
|
{ "ORGANIZATION_SIZE", demoEmailObject.OragnizationSize ?? string.Empty },
|
|
{ "INDUSTRY_NAME", demoEmailObject.IndustryName ?? string.Empty },
|
|
{ "CONTACT_NAME", demoEmailObject.ContactPerson ?? string.Empty },
|
|
{ "CONTACT_NUMBER", demoEmailObject.ContactNumber ?? string.Empty }
|
|
};
|
|
string emailBody = await GetEmailTemplate("request-demo", replacements);
|
|
|
|
await SendEmailAsync(toEmails, "User Requested a Demo", emailBody);
|
|
|
|
}
|
|
|
|
public async Task SendEmailAsync(List<string> toEmails, string subject, string body)
|
|
{
|
|
var email = new MimeMessage();
|
|
email.From.Add(new MailboxAddress(_smtpSettings.SenderName, _smtpSettings.SenderEmail));
|
|
foreach (var toEmail in toEmails)
|
|
{
|
|
email.To.Add(MailboxAddress.Parse(toEmail));
|
|
}
|
|
email.Subject = subject;
|
|
|
|
var bodyBuilder = new BodyBuilder { HtmlBody = body };
|
|
email.Body = bodyBuilder.ToMessageBody();
|
|
|
|
using var smtp = new SmtpClient();
|
|
await smtp.ConnectAsync(_smtpSettings.SmtpServer, _smtpSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
|
|
await smtp.AuthenticateAsync(_smtpSettings.SenderEmail, _smtpSettings.Password);
|
|
await smtp.SendAsync(email);
|
|
await smtp.DisconnectAsync(true);
|
|
}
|
|
}
|
|
|
|
}
|