add mail sender and basic mail templates
This commit is contained in:
parent
040d276d5b
commit
64cb4f1ac9
@ -1,4 +1,6 @@
|
|||||||
using BusinessLogic.Tools;
|
using BusinessLogic.Tools;
|
||||||
|
using BusinessLogic.Tools.Mail;
|
||||||
|
using BusinessLogic.Tools.Mail.MailTemplates;
|
||||||
using Contracts.BindingModels;
|
using Contracts.BindingModels;
|
||||||
using Contracts.BusinessLogicContracts;
|
using Contracts.BusinessLogicContracts;
|
||||||
using Contracts.Converters;
|
using Contracts.Converters;
|
||||||
@ -39,6 +41,8 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
throw new Exception("Insert operation failed.");
|
throw new Exception("Insert operation failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MailSender.Send(new MailRegistration(user));
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +56,7 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new ElementNotFoundException();
|
throw new ElementNotFoundException();
|
||||||
}
|
}
|
||||||
|
MailSender.Send(new MailDeleteUser(user));
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
@ -99,6 +104,9 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new Exception("Update operation failed.");
|
throw new Exception("Update operation failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MailSender.Send(new MailUpdateUserData(user));
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
BusinessLogic/Tools/Mail/Mail.cs
Normal file
15
BusinessLogic/Tools/Mail/Mail.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail
|
||||||
|
{
|
||||||
|
public class Mail
|
||||||
|
{
|
||||||
|
public IEnumerable<string> To { get; set; } = null!;
|
||||||
|
public string Title { get; set; } = null!;
|
||||||
|
public string Body { get; set; } = null!;
|
||||||
|
}
|
||||||
|
}
|
16
BusinessLogic/Tools/Mail/MailOptions.cs
Normal file
16
BusinessLogic/Tools/Mail/MailOptions.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail
|
||||||
|
{
|
||||||
|
public class MailOptions
|
||||||
|
{
|
||||||
|
public string Email { get; set; } = null!;
|
||||||
|
public string Password { get; set; } = null!;
|
||||||
|
public string SmtpClientHost { get; set; } = null!;
|
||||||
|
public short SmtpClientPort { get; set; }
|
||||||
|
}
|
||||||
|
}
|
46
BusinessLogic/Tools/Mail/MailSender.cs
Normal file
46
BusinessLogic/Tools/Mail/MailSender.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Mail;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail
|
||||||
|
{
|
||||||
|
public class MailSender
|
||||||
|
{
|
||||||
|
private static string _email;
|
||||||
|
private static string _password;
|
||||||
|
private static string _smtpClientHost;
|
||||||
|
private static short _smtpClientPort;
|
||||||
|
|
||||||
|
public void SetupMailOptions(MailOptions options)
|
||||||
|
{
|
||||||
|
_email = options.Email;
|
||||||
|
_password = options.Password;
|
||||||
|
_smtpClientHost = options.SmtpClientHost;
|
||||||
|
_smtpClientPort = options.SmtpClientPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Send(Mail mail)
|
||||||
|
{
|
||||||
|
using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort);
|
||||||
|
client.Credentials = new NetworkCredential(_email, _password);
|
||||||
|
client.EnableSsl = true;
|
||||||
|
|
||||||
|
using MailMessage message = new MailMessage();
|
||||||
|
|
||||||
|
message.From = new MailAddress(_email);
|
||||||
|
foreach (string to in mail.To)
|
||||||
|
{
|
||||||
|
message.To.Add(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
message.Subject = mail.Title;
|
||||||
|
message.Body = mail.Body;
|
||||||
|
|
||||||
|
client.Send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
BusinessLogic/Tools/Mail/MailTemplates/MailDeleteUser.cs
Normal file
20
BusinessLogic/Tools/Mail/MailTemplates/MailDeleteUser.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using Contracts.BindingModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail.MailTemplates
|
||||||
|
{
|
||||||
|
public class MailDeleteUser : Mail
|
||||||
|
{
|
||||||
|
public MailDeleteUser(UserBindingModel user)
|
||||||
|
{
|
||||||
|
To = [user.Email];
|
||||||
|
Title = "Ваш аккаунт был удален!";
|
||||||
|
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваш аккаунт был удален навсегда насовсем.\n" +
|
||||||
|
$"Если это были не Вы... Тут уже ничего не поможет, приносим наши извинения.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs
Normal file
21
BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using Contracts.BindingModels;
|
||||||
|
using Contracts.ViewModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail.MailTemplates
|
||||||
|
{
|
||||||
|
public class MailRegistration : Mail
|
||||||
|
{
|
||||||
|
public MailRegistration(UserBindingModel user)
|
||||||
|
{
|
||||||
|
To = [user.Email];
|
||||||
|
Title = "Приветствуем Вас на нашем сайте!";
|
||||||
|
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" +
|
||||||
|
$"Надеемся, что Вам что-то уже приглянулось!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
BusinessLogic/Tools/Mail/MailTemplates/MailUpdateUserData.cs
Normal file
20
BusinessLogic/Tools/Mail/MailTemplates/MailUpdateUserData.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using Contracts.BindingModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BusinessLogic.Tools.Mail.MailTemplates
|
||||||
|
{
|
||||||
|
public class MailUpdateUserData : Mail
|
||||||
|
{
|
||||||
|
public MailUpdateUserData(UserBindingModel user)
|
||||||
|
{
|
||||||
|
To = [user.Email];
|
||||||
|
Title = "Данные пользователя были обновлены!";
|
||||||
|
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваши данные были обвновлены.\n" +
|
||||||
|
$"Если это были не Вы, то что поделать, Вам придется менять пароль.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -56,13 +56,11 @@ namespace RestAPI.Controllers
|
|||||||
catch (AccountException ex)
|
catch (AccountException ex)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "Wrong registration data");
|
_logger.LogWarning(ex, "Wrong registration data");
|
||||||
throw;
|
|
||||||
return Results.BadRequest(ex.Message);
|
return Results.BadRequest(ex.Message);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error create user");
|
_logger.LogError(ex, "Error create user");
|
||||||
throw;
|
|
||||||
return Results.Problem(ex.Message);
|
return Results.Problem(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,13 @@ jwtProvider?.SetupJwtOptions(new()
|
|||||||
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
|
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
|
||||||
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
|
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
|
||||||
});
|
});
|
||||||
|
mailSender?.SetupMailOptions(new()
|
||||||
|
{
|
||||||
|
Email = getSection("MailOptions:Email") ?? string.Empty,
|
||||||
|
Password = getSection("MailOptions:Password") ?? string.Empty,
|
||||||
|
SmtpClientHost = getSection("MailOptions:SmtpClientHost") ?? string.Empty,
|
||||||
|
SmtpClientPort = Convert.ToInt16(getSection("MailOptions:SmtpClientPort"))
|
||||||
|
});
|
||||||
|
|
||||||
#endregion Setup config
|
#endregion Setup config
|
||||||
|
|
||||||
|
@ -9,5 +9,11 @@
|
|||||||
"SecretKey": "secretkey_secretkey_secretkey_secretkey",
|
"SecretKey": "secretkey_secretkey_secretkey_secretkey",
|
||||||
"ExpiresHours": 24
|
"ExpiresHours": 24
|
||||||
},
|
},
|
||||||
|
"MailOptions": {
|
||||||
|
"Email": "21.guns.site@gmail.com",
|
||||||
|
"Password": "tiss lpgf nhap qnfc",
|
||||||
|
"SmtpClientHost": "smtp.gmail.com",
|
||||||
|
"SmtpClientPort": "587"
|
||||||
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user