diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj index 2f4fc77..c2d29fd 100644 --- a/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -7,4 +7,10 @@ enable + + + Always + + + diff --git a/ConsoleApp1/MailService.cs b/ConsoleApp1/MailService.cs new file mode 100644 index 0000000..20d056c --- /dev/null +++ b/ConsoleApp1/MailService.cs @@ -0,0 +1,35 @@ +using System.Net; +using System.Net.Mail; + +namespace ConsoleApp1; + +public class MailService +{ + private readonly SmtpSettings _smtpSettings; + + public MailService(SmtpSettings smtpSettings) + { + _smtpSettings = smtpSettings; + } + + public void SendEmail(string to, string subject, string body) + { + using (var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort)) + { + client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword); + client.EnableSsl = true; + + var mailMessage = new MailMessage + { + From = new MailAddress(_smtpSettings.SmtpFrom), + Subject = subject, + Body = body, + IsBodyHtml = true, + }; + + mailMessage.To.Add(to); + + client.Send(mailMessage); + } + } +} \ No newline at end of file diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index c7ba02b..e046fe0 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -1,9 +1,32 @@ -namespace ConsoleApp1; +using System; +using System.IO; +using System.Text.Json; -class Program +namespace ConsoleApp1 { - static void Main(string[] args) + class Program { - Console.WriteLine("Hello, World!"); + static void Main(string[] args) + { + string jsonFilePath = "appsettings.json"; + string jsonString = File.ReadAllText(jsonFilePath); + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + SmtpSettings? smtpSettings = JsonSerializer.Deserialize(jsonString, options); + + if (smtpSettings is null) + { + Console.WriteLine("Failed to deserialize SmtpSettings"); + return; + } + + MailService mailService = new MailService(smtpSettings); + mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!"); + + } } } \ No newline at end of file diff --git a/ConsoleApp1/SmtpSettings.cs b/ConsoleApp1/SmtpSettings.cs new file mode 100644 index 0000000..7f2dcf3 --- /dev/null +++ b/ConsoleApp1/SmtpSettings.cs @@ -0,0 +1,10 @@ +namespace ConsoleApp1; + +public class SmtpSettings +{ + public string SmtpServer { get; set; } + public int SmtpPort { get; set; } + public string SmtpUser { get; set; } + public string SmtpPassword { get; set; } + public string SmtpFrom { get; set; } +} \ No newline at end of file