48 lines
2.0 KiB
C#
48 lines
2.0 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 readonly SendGridClient _sendGridClient;
|
|
private readonly EmailAddress _fromEmailAddress;
|
|
|
|
public AcEmailServiceServer(IConfiguration configuration) : this()
|
|
{
|
|
_sendGridClient = new SendGridClient(configuration["SendGrid:Key"]);
|
|
_fromEmailAddress = new EmailAddress(configuration["SendGrid:FromEmail"], configuration["SendGrid:FromName"]);
|
|
|
|
//Logger.Info($"{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);
|
|
}
|
|
} |