From 7c385bc51cc4f3da767f9e9aaff67a9d3a9ba844 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 13:37:35 +0100 Subject: [PATCH] added smtpconfig class and put smtp config data into resources json file --- csharp/App/Backend/Mailer/Mailer.cs | 12 ++++++++---- csharp/App/Backend/Mailer/SmptConfig.cs | 13 +++++++++++++ csharp/App/Backend/Resources/smtpConfig.json | 6 ++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 csharp/App/Backend/Mailer/SmptConfig.cs create mode 100644 csharp/App/Backend/Resources/smtpConfig.json diff --git a/csharp/App/Backend/Mailer/Mailer.cs b/csharp/App/Backend/Mailer/Mailer.cs index 3d038fe2b..ae3ce7f0b 100644 --- a/csharp/App/Backend/Mailer/Mailer.cs +++ b/csharp/App/Backend/Mailer/Mailer.cs @@ -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 { - public static Boolean SendVerificationMessage (User emailRecipientUser) + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] + public static Boolean SendVerificationMessage (User emailRecipientUser) { + var config = JsonSerializer.Deserialize(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); diff --git a/csharp/App/Backend/Mailer/SmptConfig.cs b/csharp/App/Backend/Mailer/SmptConfig.cs new file mode 100644 index 000000000..c52bbc251 --- /dev/null +++ b/csharp/App/Backend/Mailer/SmptConfig.cs @@ -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() + { } + +} \ No newline at end of file diff --git a/csharp/App/Backend/Resources/smtpConfig.json b/csharp/App/Backend/Resources/smtpConfig.json new file mode 100644 index 000000000..9b4024e59 --- /dev/null +++ b/csharp/App/Backend/Resources/smtpConfig.json @@ -0,0 +1,6 @@ +{ + "Url": "smtp.ethereal.email", + "Port": 587, + "Username": "fern95@ethereal.email", + "Password": "dYKVnc4RQNEFckHaNV" +} \ No newline at end of file