50 lines
2.2 KiB
C#
50 lines
2.2 KiB
C#
using AyCode.Core.Consts;
|
|
using Microsoft.Extensions.Configuration;
|
|
using SendGrid.Helpers.Mail;
|
|
using SendGrid;
|
|
|
|
namespace AyCode.Services.Server.Emails;
|
|
|
|
public interface IAcEmailServiceServer
|
|
{
|
|
|
|
}
|
|
public class AcEmailServiceServer() : IAcEmailServiceServer
|
|
{
|
|
private IConfiguration _configuration;
|
|
private readonly SendGridClient _sendGridClient;
|
|
private readonly EmailAddress _fromEmailAddress;
|
|
|
|
public AcEmailServiceServer(IConfiguration configuration) : this()
|
|
{
|
|
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
//_sendGridClient = new SendGridClient(_configuration.ServerUserName);
|
|
//_fromEmailAddress = new EmailAddress(_configuration.FromEmail, _configuration.FromName);
|
|
|
|
//Console.WriteLine($"{config.ServerUserName}; {config.FromEmail}; {config.FromName}");
|
|
}
|
|
|
|
public async Task<Response> SendLostPasswordEmailAsync(string addressEmail, string verificationToken)
|
|
{
|
|
const string subject = "Lost password";
|
|
var toEmailAddress = new EmailAddress(addressEmail);
|
|
|
|
var plainTextContent = $"{verificationToken}";
|
|
var htmlContent = $"<strong>{verificationToken}</strong>";
|
|
|
|
var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject, plainTextContent, htmlContent);
|
|
return await _sendGridClient.SendEmailAsync(msg).ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task<Response> SendRegistrationEmailAsync(string addressEmail, string verificationToken, string userName)
|
|
{
|
|
const string subject = "Registration";
|
|
var toEmailAddress = new EmailAddress(addressEmail);
|
|
|
|
var plainTextContent = $"User name: {userName}{AcEnv.NL}Confirmation token: {verificationToken}";
|
|
var htmlContent = $"User name: <strong>{userName}</strong></br>Confirmation token: <strong>{verificationToken}</strong>";
|
|
|
|
var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject, plainTextContent, htmlContent);
|
|
return await _sendGridClient.SendEmailAsync(msg).ConfigureAwait(false);
|
|
}
|
|
} |