Merge branch 'fix/postit-acl-and-pub' into release/1.0.8-rc6
All checks were successful
Dotnet build and test / build (push) Successful in 7m31s
All checks were successful
Dotnet build and test / build (push) Successful in 7m31s
This commit is contained in:
commit
1b237fa540
16 changed files with 385 additions and 229 deletions
|
|
@ -9,6 +9,7 @@ using Yavsc.Settings;
|
|||
using Yavsc.Models;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
|
||||
namespace Yavsc.Services
|
||||
|
|
@ -53,45 +54,96 @@ namespace Yavsc.Services
|
|||
/// </returns>
|
||||
public Task SendEmailAsync(string email, string subject, string htmlMessage)
|
||||
{
|
||||
return SendEmailAsync("", email, subject, htmlMessage);
|
||||
return SendEmailAsync(null, email, subject, htmlMessage);
|
||||
}
|
||||
|
||||
internal static MailboxAddress BuildMailboxAddress(string? displayName, string? rawAddress)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rawAddress))
|
||||
{
|
||||
throw new FormatException("Email address is empty.");
|
||||
}
|
||||
|
||||
var candidate = rawAddress.Trim();
|
||||
if (candidate.Contains('<') || candidate.Contains('>'))
|
||||
{
|
||||
var emailMatch = Regex.Match(candidate,
|
||||
@"[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}",
|
||||
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
if (emailMatch.Success)
|
||||
{
|
||||
candidate = emailMatch.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
candidate = candidate.Trim('<', '>', '"', '\'');
|
||||
}
|
||||
}
|
||||
|
||||
candidate = candidate.Trim('"', '\'', '<', '>', ' ');
|
||||
candidate = candidate.Replace(" ", string.Empty);
|
||||
|
||||
if (!MailboxAddress.TryParse(candidate, out var parsedAddress))
|
||||
{
|
||||
throw new FormatException($"Invalid email address '{rawAddress}'.");
|
||||
}
|
||||
|
||||
var safeName = string.IsNullOrWhiteSpace(displayName)
|
||||
? parsedAddress.Name
|
||||
: displayName.Trim();
|
||||
|
||||
return new MailboxAddress(safeName ?? string.Empty, parsedAddress.Address);
|
||||
}
|
||||
|
||||
public async Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
|
||||
{
|
||||
logger.LogInformation($"SendEmail for {email} : {subject}");
|
||||
MimeMessage msg = new();
|
||||
msg.From.Add(new MailboxAddress(siteSettings.Owner.Name,
|
||||
siteSettings.Owner.EMail));
|
||||
msg.To.Add(new MailboxAddress(name, email));
|
||||
TextPart text;
|
||||
msg.Body = text = new TextPart("html")
|
||||
try
|
||||
{
|
||||
Text = $"<html><body>{htmlMessage}</body></html>"
|
||||
};
|
||||
|
||||
msg.Subject = subject;
|
||||
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
|
||||
siteSettings.Authority
|
||||
);
|
||||
using ISmtpClient sc = _smtpClientFactory.CreateClient();
|
||||
{
|
||||
sc.Timeout = 30000;
|
||||
sc.Connect(
|
||||
smtpSettings.Host,
|
||||
smtpSettings.Port,
|
||||
SecureSocketOptions.Auto
|
||||
);
|
||||
|
||||
if (smtpSettings.UserName != null)
|
||||
logger.LogInformation($"SendEmail for {email} : {subject}");
|
||||
MimeMessage msg = new();
|
||||
msg.From.Add(BuildMailboxAddress(siteSettings.Owner.Name, siteSettings.Owner.EMail));
|
||||
msg.To.Add(BuildMailboxAddress(name, email));
|
||||
TextPart text;
|
||||
msg.Body = text = new TextPart("html")
|
||||
{
|
||||
sc.Authenticate(smtpSettings.UserName, smtpSettings.Password);
|
||||
}
|
||||
Text = $"<html><body>{htmlMessage}</body></html>"
|
||||
};
|
||||
|
||||
await sc.SendAsync(msg);
|
||||
logger.LogInformation($"Sent : {msg.MessageId}");
|
||||
sc.Disconnect(true);
|
||||
msg.Subject = subject;
|
||||
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
|
||||
siteSettings.Authority
|
||||
);
|
||||
using ISmtpClient sc = _smtpClientFactory.CreateClient();
|
||||
{
|
||||
sc.Timeout = 30000;
|
||||
sc.Connect(
|
||||
smtpSettings.Host,
|
||||
smtpSettings.Port,
|
||||
SecureSocketOptions.Auto
|
||||
);
|
||||
|
||||
if (smtpSettings.UserName != null)
|
||||
{
|
||||
sc.Authenticate(smtpSettings.UserName, smtpSettings.Password);
|
||||
}
|
||||
|
||||
await sc.SendAsync(msg);
|
||||
logger.LogInformation($"Sent : {msg.MessageId}");
|
||||
sc.Disconnect(true);
|
||||
}
|
||||
return msg.MessageId;
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
logger.LogError(ex, "Refusing to send email because the recipient or sender address is malformed. To={To}, From={From}", email, siteSettings.Owner.EMail);
|
||||
return string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to send email. To={To}, Subject={Subject}", email, subject);
|
||||
throw;
|
||||
}
|
||||
return msg.MessageId;
|
||||
}
|
||||
|
||||
public void SendEmailFromCriteria(string Criteria)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue