yavsc/src/Yavsc.Server/Services/MailSender.cs

118 lines
4.2 KiB
C#
Raw Normal View History

using System.Net;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Logging;
2023-03-19 17:57:55 +00:00
using Microsoft.Extensions.Options;
using MimeKit;
2025-03-02 16:53:36 +00:00
using Microsoft.AspNetCore.Identity;
2023-04-05 22:29:40 +01:00
using Yavsc.Interface;
using Yavsc.Settings;
2025-03-02 16:53:36 +00:00
using Yavsc.Models;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Localization;
using System.Web;
namespace Yavsc.Services
{
2025-09-07 17:43:13 +01:00
public class MailSender : IEmailSender<ApplicationUser>, IEmailSender, ITrueEmailSender
{
2025-08-31 18:27:53 +01:00
private readonly IStringLocalizer<MailSender> localizer;
2020-09-12 01:11:30 +01:00
readonly SiteSettings siteSettings;
readonly SmtpSettings smtpSettings;
2023-03-19 16:02:50 +00:00
private readonly ILogger logger;
public MailSender(
2025-09-07 17:43:13 +01:00
IOptions<SiteSettings> sitesOptions,
2023-03-19 16:02:50 +00:00
IOptions<SmtpSettings> smtpOptions,
2025-08-31 18:27:53 +01:00
ILoggerFactory loggerFactory,
IStringLocalizer<MailSender> localizer
)
{
2025-03-02 16:53:36 +00:00
this.localizer = localizer;
2023-04-05 22:29:40 +01:00
siteSettings = sitesOptions.Value;
smtpSettings = smtpOptions.Value;
2023-03-19 16:02:50 +00:00
logger = loggerFactory.CreateLogger<MailSender>();
}
2025-03-02 16:53:36 +00:00
public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="googleSettings"></param>
/// <param name="registrationId"></param>
/// <param name="ev"></param>
/// <returns>a MessageWithPayloadResponse,
/// <c>bool somethingsent = (response.failure == 0 &amp;&amp; response.success > 0)</c>
/// </returns>
2026-04-20 00:35:51 +01:00
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
2026-04-20 00:35:51 +01:00
return SendEmailAsync("", email, subject, htmlMessage);
2023-04-05 22:29:40 +01:00
}
public async Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
{
2025-09-07 17:43:13 +01:00
logger.LogInformation($"SendEmail for {email} : {subject}");
MimeMessage msg = new();
2023-04-05 22:29:40 +01:00
msg.From.Add(new MailboxAddress(siteSettings.Owner.Name,
siteSettings.Owner.EMail));
msg.To.Add(new MailboxAddress(name, email));
2026-02-22 23:39:56 +00:00
TextPart text;
msg.Body = text = new TextPart("html")
{
2026-02-22 23:39:56 +00:00
Text = $"<html><body>{htmlMessage}</body></html>"
2023-04-05 22:29:40 +01:00
};
2026-02-22 23:39:56 +00:00
2023-04-05 22:29:40 +01:00
msg.Subject = subject;
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
siteSettings.Authority
);
2025-09-07 17:43:13 +01:00
using (SmtpClient sc = new())
{
2026-04-20 00:35:51 +01:00
sc.Timeout = 30000;
2023-04-05 22:29:40 +01:00
sc.Connect(
2026-04-20 00:35:51 +01:00
smtpSettings.Host,
2023-04-05 22:29:40 +01:00
smtpSettings.Port,
2025-09-07 17:43:13 +01:00
SecureSocketOptions.Auto
);
if (smtpSettings.UserName != null)
{
sc.Authenticate(smtpSettings.UserName, smtpSettings.Password);
2023-04-05 22:29:40 +01:00
}
2025-09-07 17:43:13 +01:00
2023-04-05 22:29:40 +01:00
await sc.SendAsync(msg);
logger.LogInformation($"Sent : {msg.MessageId}");
2025-09-07 17:43:13 +01:00
sc.Disconnect(true);
}
2023-04-05 22:29:40 +01:00
return msg.MessageId;
}
2025-03-02 16:53:36 +00:00
2025-07-16 00:47:50 +01:00
public void SendEmailFromCriteria(string Criteria)
{
throw new NotImplementedException();
}
2025-03-02 16:53:36 +00:00
public async Task SendPasswordResetCodeAsync(ApplicationUser user, string email, string resetCode)
{
2025-09-07 17:43:13 +01:00
var callbackUrl = siteSettings.Audience + "/Account/ResetPassword/" +
2025-03-02 16:53:36 +00:00
HttpUtility.UrlEncode(user.Id) + "/" + HttpUtility.UrlEncode(resetCode);
2025-09-07 17:43:13 +01:00
await SendEmailAsync(user.UserName, user.Email,
2025-03-02 16:53:36 +00:00
localizer["Reset Password"],
localizer["Please reset your password by "] + " <a href=\"" +
callbackUrl + "\" >following this link</a>");
throw new NotImplementedException();
}
public async Task SendPasswordResetLinkAsync(ApplicationUser user, string email, string resetLink)
{
2025-09-07 17:43:13 +01:00
await SendEmailAsync(user.UserName, user.Email,
localizer["Reset Password"],
localizer["Please reset your password by "] + " <a href=\"" +
resetLink + "\" >following this link</a>");
2025-03-02 16:53:36 +00:00
}
}
}