Got a login
This commit is contained in:
parent
c771d26995
commit
4b52b6e96e
61 changed files with 7793 additions and 609 deletions
|
|
@ -1,3 +1,4 @@
|
|||
using System.Text;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
|
@ -7,10 +8,13 @@ using Microsoft.Extensions.Logging;
|
|||
using Microsoft.Extensions.Options;
|
||||
using MimeKit;
|
||||
using Yavsc.Abstract.Manage;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Yavsc.Interface;
|
||||
using Yavsc.Settings;
|
||||
|
||||
namespace Yavsc.Services
|
||||
{
|
||||
public class MailSender : IEmailSender
|
||||
public class MailSender : IEmailSender, ITrueEmailSender
|
||||
{
|
||||
readonly SiteSettings siteSettings;
|
||||
readonly SmtpSettings smtpSettings;
|
||||
|
|
@ -22,10 +26,9 @@ namespace Yavsc.Services
|
|||
ILoggerFactory loggerFactory
|
||||
)
|
||||
{
|
||||
siteSettings = sitesOptions?.Value;
|
||||
smtpSettings = smtpOptions?.Value;
|
||||
siteSettings = sitesOptions.Value;
|
||||
smtpSettings = smtpOptions.Value;
|
||||
logger = loggerFactory.CreateLogger<MailSender>();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -39,52 +42,42 @@ namespace Yavsc.Services
|
|||
/// </returns>
|
||||
|
||||
|
||||
public async Task<EmailSentViewModel> SendEmailAsync(string username, string email, string subject, string message)
|
||||
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
|
||||
{
|
||||
EmailSentViewModel model = new EmailSentViewModel{ EMail = email };
|
||||
try
|
||||
{
|
||||
logger.LogInformation($"sendEmail for {email} : {message}");
|
||||
MimeMessage msg = new MimeMessage();
|
||||
msg.From.Add(new MailboxAddress(
|
||||
siteSettings.Owner.Name,
|
||||
siteSettings.Owner.EMail));
|
||||
msg.To.Add(new MailboxAddress(username, email));
|
||||
msg.Body = new TextPart("plain")
|
||||
{
|
||||
Text = message
|
||||
};
|
||||
msg.Subject = subject;
|
||||
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
|
||||
siteSettings.Authority
|
||||
);
|
||||
using (SmtpClient sc = new SmtpClient())
|
||||
{
|
||||
sc.Connect(
|
||||
smtpSettings.Host,
|
||||
smtpSettings.Port,
|
||||
SecureSocketOptions.Auto);
|
||||
|
||||
if (smtpSettings.UserName!=null) {
|
||||
NetworkCredential creds = new NetworkCredential(
|
||||
smtpSettings.UserName, smtpSettings.Password, smtpSettings.Domain);
|
||||
await sc.AuthenticateAsync(System.Text.Encoding.UTF8, creds, System.Threading.CancellationToken.None);
|
||||
}
|
||||
|
||||
await sc.SendAsync(msg);
|
||||
model.MessageId = msg.MessageId;
|
||||
model.Sent = true; // a duplicate info to remove from the view model, that equals to MessageId == null
|
||||
logger.LogInformation($"Sent : {msg}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
model.Sent = false;
|
||||
model.ErrorMessage = ex.Message;
|
||||
return model;
|
||||
}
|
||||
return model;
|
||||
await SendEmailAsync("", email, subject, htmlMessage);
|
||||
}
|
||||
|
||||
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));
|
||||
msg.Body = new TextPart("html")
|
||||
{
|
||||
Text = htmlMessage
|
||||
};
|
||||
msg.Subject = subject;
|
||||
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
|
||||
siteSettings.Authority
|
||||
);
|
||||
using (SmtpClient sc = new ())
|
||||
{
|
||||
sc.Connect(
|
||||
smtpSettings.Server,
|
||||
smtpSettings.Port,
|
||||
SecureSocketOptions.Auto);
|
||||
|
||||
if (smtpSettings.UserName!=null) {
|
||||
NetworkCredential creds = new (
|
||||
smtpSettings.UserName, smtpSettings.Password);
|
||||
await sc.AuthenticateAsync(System.Text.Encoding.UTF8, creds, System.Threading.CancellationToken.None);
|
||||
}
|
||||
await sc.SendAsync(msg);
|
||||
logger.LogInformation($"Sent : {msg.MessageId}");
|
||||
}
|
||||
return msg.MessageId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue