Skip to content

Instantly share code, notes, and snippets.

@kolosovpetro
Created December 31, 2021 17:21
Show Gist options
  • Select an option

  • Save kolosovpetro/742c61cee2562bca92ed0799f8955f18 to your computer and use it in GitHub Desktop.

Select an option

Save kolosovpetro/742c61cee2562bca92ed0799f8955f18 to your computer and use it in GitHub Desktop.
using MangoAPI.Application.Interfaces;
using MangoAPI.Domain.Constants;
using MangoAPI.Domain.Entities;
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MangoAPI.Application.Services
{
public class GmailSmtpSenderService : IEmailSenderService
{
public async Task SendVerificationEmailAsync(UserEntity user, CancellationToken cancellationToken)
{
var senderEmail = EnvironmentConstants.MangoEmailNotificationsAddress;
var smtpClient = GetGmailSmtpClient();
var verificationMessage = EmailMessages.GetVerificationMessage(senderEmail, user);
await smtpClient.SendMailAsync(verificationMessage, cancellationToken);
}
public async Task SendPasswordRestoreRequest(UserEntity user, Guid requestId,
CancellationToken cancellationToken)
{
var senderEmail = EnvironmentConstants.MangoEmailNotificationsAddress;
var smtpClient = GetGmailSmtpClient();
var verificationMessage = EmailMessages.GetPasswordRestoreMessage(senderEmail, user, requestId);
await smtpClient.SendMailAsync(verificationMessage, cancellationToken);
}
private static SmtpClient GetGmailSmtpClient()
{
var senderEmail = EnvironmentConstants.MangoEmailNotificationsAddress;
var senderPassword = EnvironmentConstants.MangoEmailNotificationsPassword;
var credentials = new NetworkCredential(senderEmail, senderPassword);
var smtpClient = new SmtpClient
{
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = credentials,
Host = GmailConstants.GmailSmtpHost,
Port = GmailConstants.GmailPort,
DeliveryMethod = SmtpDeliveryMethod.Network
};
return smtpClient;
}
}
public static class EmailMessages
{
public static MailMessage GetVerificationMessage(string senderEmail, UserEntity user)
{
var fromAddress = new MailAddress(senderEmail);
var toAddress = new MailAddress(user.Email, user.DisplayName, Encoding.UTF8);
var message = new MailMessage(fromAddress, toAddress)
{
Subject = "Email Verification",
SubjectEncoding = Encoding.UTF8,
};
var body = GenerateEmailConfirmBody(user);
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.Body = body;
return message;
}
private static string GenerateEmailConfirmBody(UserEntity user)
{
var body =
"<!DOCTYPE html>" +
"<head>" +
"<meta charset='utf-8'>" +
"</head>" +
"<body>" +
$"<p>Hi, {user.DisplayName}, please follow this link.</p>" +
"<br>" +
$"<a href='{EnvironmentConstants.MangoFrontendAddress}/verify-email?email={user.Email}&emailCode={user.EmailCode}'>" +
"Verify email" +
"</a>" +
"<br>" +
$"<p>Confirmation Code: {user.EmailCode}</p>" +
"</body>";
return body;
}
public static MailMessage GetPasswordRestoreMessage(string senderEmail, UserEntity user, Guid requestId)
{
var fromAddress = new MailAddress(senderEmail);
var toAddress = new MailAddress(user.Email, user.DisplayName, Encoding.UTF8);
var message = new MailMessage(fromAddress, toAddress)
{
Subject = "Password Restore Request",
SubjectEncoding = Encoding.UTF8,
};
var body = GeneratePasswordRestoreRequestBody(user, requestId);
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.Body = body;
return message;
}
private static string GeneratePasswordRestoreRequestBody(UserEntity user, Guid requestId)
{
var body =
"<!DOCTYPE html>" +
"<head>" +
"<meta charset='utf-8'>" +
"</head>" +
"<body>" +
$"<p>Hi, {user.DisplayName}, please follow the link to restore password.</p>" +
"<br>" +
$"<a href='{EnvironmentConstants.MangoFrontendAddress}restore-password-form?requestId={requestId}'>" +
"Restore Password" +
"</a>" +
"<br>" +
$"<p>Request ID: {requestId}.</p>" +
"</body>";
return body;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment