added smtpconfig class and put smtp config data into resources json file

This commit is contained in:
Kim 2023-03-23 13:37:35 +01:00
parent 432189f461
commit 7c385bc51c
3 changed files with 27 additions and 4 deletions

View File

@ -1,16 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using InnovEnergy.App.Backend.DataTypes;
using MailKit.Net.Smtp;
using MimeKit;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace InnovEnergy.App.Backend.Mailer;
public static class Mailer
{
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static Boolean SendVerificationMessage (User emailRecipientUser)
{
var config = JsonSerializer.Deserialize<SmptConfig>(File.OpenRead("./Resources/smtpConfig.json"))!;
var email = new MimeMessage();
email.From.Add(new MailboxAddress("InnovEnergy", "noreply@innov.energy"));
email.To.Add(new MailboxAddress(emailRecipientUser.Name, "fern95@ethereal.email"));
email.To.Add(new MailboxAddress(emailRecipientUser.Name, "fern95@ethereal.email")); //TODO CHANGE ME
email.Subject = "Create a new password for your Innovenergy-Account";
email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) {
@ -19,10 +23,10 @@ public static class Mailer
};
using (var smtp = new SmtpClient())
{
smtp.Connect("smtp.ethereal.email", 587, false);
smtp.Connect(config.Url, config.Port, false);
// Todo put me into urlAndKey.json
smtp.Authenticate("fern95@ethereal.email", "dYKVnc4RQNEFckHaNV");
smtp.Authenticate(config.Username, config.Password);
smtp.Send(email);
smtp.Disconnect(true);

View File

@ -0,0 +1,13 @@
namespace InnovEnergy.App.Backend.Mailer;
public class SmptConfig
{
public String Url { get; init; }
public String Username { get; init; }
public String Password { get; init; }
public Int32 Port { get; init; }
public SmptConfig()
{ }
}

View File

@ -0,0 +1,6 @@
{
"Url": "smtp.ethereal.email",
"Port": 587,
"Username": "fern95@ethereal.email",
"Password": "dYKVnc4RQNEFckHaNV"
}