From 1020aa64493578cd52fdf4fa95ce57cd3704486e Mon Sep 17 00:00:00 2001 From: huetruong100802 Date: Thu, 10 Oct 2024 20:26:22 +0700 Subject: [PATCH 1/3] Add Mail Services --- MBS-COMMAND.API/appsettings.Development.json | 7 +++ MBS-COMMAND.API/appsettings.json | 8 +++ .../Abstractions/IMailService.cs | 14 +++++ .../Extensions/ServiceCollectionExtensions.cs | 14 ++++- .../DependencyInjection/Options/MailOption.cs | 13 ++++ .../MBS_COMMAND.Infrastucture.csproj | 2 + MBS_COMMAND.Infrastucture/Mail/MailService.cs | 59 +++++++++++++++++++ 7 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 MBS_COMMAND.Application/Abstractions/IMailService.cs create mode 100644 MBS_COMMAND.Infrastucture/DependencyInjection/Options/MailOption.cs create mode 100644 MBS_COMMAND.Infrastucture/Mail/MailService.cs diff --git a/MBS-COMMAND.API/appsettings.Development.json b/MBS-COMMAND.API/appsettings.Development.json index b8ba90e..e4789f2 100644 --- a/MBS-COMMAND.API/appsettings.Development.json +++ b/MBS-COMMAND.API/appsettings.Development.json @@ -21,6 +21,13 @@ "initialInterval": "00:00:05", "intervalIncrement": "00:00:10" }, + "MailOption": { + "Mail": "contact.antreevn@gmail.com", + "DisplayName": "Antree", + "Password": "aymfdegtavaixpyo", + "Host": "smtp.gmail.com", + "Port": 587 + }, "CloudinaryOptions" : { "CloudName": "dejf8mmou", "ApiKey": "352838987568689", diff --git a/MBS-COMMAND.API/appsettings.json b/MBS-COMMAND.API/appsettings.json index ae7133f..ed836f2 100644 --- a/MBS-COMMAND.API/appsettings.json +++ b/MBS-COMMAND.API/appsettings.json @@ -26,6 +26,14 @@ "ApiKey": "352838987568689", "ApiSecret": "a3GLia6ovHgc-8xit46QEs8usWY" }, + , + "MailOption": { + "Mail": "contact.antreevn@gmail.com", + "DisplayName": "Antree", + "Password": "aymfdegtavaixpyo", + "Host": "smtp.gmail.com", + "Port": 587 + }, "SqlServerRetryOptions": { "MaxRetryCount": 5, "MaxRetryDelay": "00:00:05", diff --git a/MBS_COMMAND.Application/Abstractions/IMailService.cs b/MBS_COMMAND.Application/Abstractions/IMailService.cs new file mode 100644 index 0000000..cb1a988 --- /dev/null +++ b/MBS_COMMAND.Application/Abstractions/IMailService.cs @@ -0,0 +1,14 @@ +namespace MBS_COMMAND.Application.Abstractions; + +public interface IMailService +{ + Task SendMail(MailContent mailContent); +} + +public class MailContent +{ + public string? To { get; set; } // Địa chỉ gửi đến + public string? Subject { get; set; } // Chủ đề (tiêu đề email) + public string? Body { get; set; } // Nội dung (hỗ trợ HTML) của email + +} \ No newline at end of file diff --git a/MBS_COMMAND.Infrastucture/DependencyInjection/Extensions/ServiceCollectionExtensions.cs b/MBS_COMMAND.Infrastucture/DependencyInjection/Extensions/ServiceCollectionExtensions.cs index 13eed4c..94db6d8 100644 --- a/MBS_COMMAND.Infrastucture/DependencyInjection/Extensions/ServiceCollectionExtensions.cs +++ b/MBS_COMMAND.Infrastucture/DependencyInjection/Extensions/ServiceCollectionExtensions.cs @@ -9,6 +9,7 @@ using Newtonsoft.Json; using Quartz; using MBS_COMMAND.Infrastucture.Caching; +using MBS_COMMAND.Infrastucture.Mail; using MBS_COMMAND.Infrastucture.DependencyInjection.Options; using MBS_COMMAND.Infrastucture.PipeObservers; using MBS_COMMAND.Infrastucture.Media; @@ -19,8 +20,10 @@ namespace MBS_COMMAND.Infrastucture.DependencyInjection.Extensions; public static class ServiceCollectionExtensions { public static void AddServicesInfrastructure(this IServiceCollection services) - => services.AddTransient() - .AddTransient() + => services + .AddTransient() + .AddSingleton() + .AddSingleton() .AddSingleton((provider) => { var options = provider.GetRequiredService>(); @@ -137,6 +140,13 @@ public static OptionsBuilder ConfigureCloudinaryOptionsInfras .ValidateDataAnnotations() .ValidateOnStart(); + public static OptionsBuilder ConfigureMailOptionsInfrastucture(this IServiceCollection services, IConfigurationSection section) + => services + .AddOptions() + .Bind(section) + .ValidateDataAnnotations() + .ValidateOnStart(); + // Configure MediatR public static void AddMediatRInfrastructure(this IServiceCollection services) { diff --git a/MBS_COMMAND.Infrastucture/DependencyInjection/Options/MailOption.cs b/MBS_COMMAND.Infrastucture/DependencyInjection/Options/MailOption.cs new file mode 100644 index 0000000..fca39f1 --- /dev/null +++ b/MBS_COMMAND.Infrastucture/DependencyInjection/Options/MailOption.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace MBS_COMMAND.Infrastucture.DependencyInjection.Options; + +public class MailOption +{ + + [Required]public string Mail { get; set; } + [Required]public string DisplayName { get; set; } + [Required]public string Password { get; set; } + [Required]public string Host { get; set; } + [Required]public int Port { get; set; } +} \ No newline at end of file diff --git a/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj b/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj index dc325c7..fe76485 100644 --- a/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj +++ b/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj @@ -16,10 +16,12 @@ + + diff --git a/MBS_COMMAND.Infrastucture/Mail/MailService.cs b/MBS_COMMAND.Infrastucture/Mail/MailService.cs new file mode 100644 index 0000000..522aeaf --- /dev/null +++ b/MBS_COMMAND.Infrastucture/Mail/MailService.cs @@ -0,0 +1,59 @@ +using MailKit.Security; +using MBS_COMMAND.Application.Abstractions; +using MBS_COMMAND.Infrastucture.DependencyInjection.Options; +using Microsoft.Extensions.Options; +using MimeKit; + +namespace MBS_COMMAND.Infrastucture.Mail; + +public class MailService : IMailService +{ + private readonly MailOption _mailOptions; + + public MailService(IOptions mailOptions) + { + _mailOptions = mailOptions.Value; + } + + public async Task SendMail(MailContent mailContent) + { + MimeMessage email = new(); + email.Sender = new MailboxAddress(_mailOptions?.DisplayName, _mailOptions?.Mail); + email.From.Add(new MailboxAddress(_mailOptions?.DisplayName, _mailOptions?.Mail)); + email.To.Add(MailboxAddress.Parse(mailContent.To)); + email.Subject = mailContent.Subject; + + + BodyBuilder builder = new(); + builder.HtmlBody = mailContent.Body; + email.Body = builder.ToMessageBody(); + + // dùng SmtpClient của MailKit + using MailKit.Net.Smtp.SmtpClient smtp = new(); + + await smtp.ConnectAsync(_mailOptions?.Host, _mailOptions!.Port, SecureSocketOptions.StartTls); + await smtp.AuthenticateAsync(_mailOptions.Mail, _mailOptions.Password); + await smtp.SendAsync(email); + + // try + // { + // smtp.Connect(_mailOptions?.Host, _mailOptions!.Port, SecureSocketOptions.StartTls); + // smtp.Authenticate(_mailOptions.Mail, _mailOptions.Password); + // await smtp.SendAsync(email); + // } + // catch (Exception ex) + // { + // // Gửi mail thất bại, nội dung email sẽ lưu vào thư mục mailssave + // // System.IO.Directory.CreateDirectory("mailssave"); + // // var emailsavefile = string.Format(@"mailssave/{0}.eml", Guid.NewGuid()); + // // await email.WriteToAsync(emailsavefile); + // + // System.Console.WriteLine("errors: ", ex); + // + // // logger.LogInformation("Lỗi gửi mail, lưu tại - " + emailsavefile); + // // logger.LogError(ex.Message); + // } + + await smtp.DisconnectAsync(true); + } +} \ No newline at end of file From 7ecd2cf05bb89a004880018b8663c30133fc7d47 Mon Sep 17 00:00:00 2001 From: huetruong100802 Date: Thu, 10 Oct 2024 20:27:02 +0700 Subject: [PATCH 2/3] Add Mail Service DependencyInjection --- MBS-COMMAND.API/Program.cs | 1 + MBS-COMMAND.API/appsettings.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/MBS-COMMAND.API/Program.cs b/MBS-COMMAND.API/Program.cs index 81d7119..cb0a646 100644 --- a/MBS-COMMAND.API/Program.cs +++ b/MBS-COMMAND.API/Program.cs @@ -73,6 +73,7 @@ builder.Services.AddServicesInfrastructure(); builder.Services.AddRedisInfrastructure(builder.Configuration); builder.Services.ConfigureCloudinaryOptionsInfrastucture(builder.Configuration.GetSection(nameof(CloudinaryOptions))); +builder.Services.ConfigureMailOptionsInfrastucture(builder.Configuration.GetSection(nameof(MailOption))); // Add Middleware => Remember using middleware builder.Services.AddTransient(); diff --git a/MBS-COMMAND.API/appsettings.json b/MBS-COMMAND.API/appsettings.json index ed836f2..4ecb220 100644 --- a/MBS-COMMAND.API/appsettings.json +++ b/MBS-COMMAND.API/appsettings.json @@ -26,7 +26,6 @@ "ApiKey": "352838987568689", "ApiSecret": "a3GLia6ovHgc-8xit46QEs8usWY" }, - , "MailOption": { "Mail": "contact.antreevn@gmail.com", "DisplayName": "Antree", From 90605ac265922600884513546f63150062b91fba Mon Sep 17 00:00:00 2001 From: bentran1vn Date: Thu, 10 Oct 2024 21:16:10 +0700 Subject: [PATCH 3/3] Add Missing Change --- MBS_COMMAND.Domain/Entities/MentorSkills.cs | 2 -- MBS_COMMAND.Domain/Entities/Schedule.cs | 3 +-- MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj | 1 + 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/MBS_COMMAND.Domain/Entities/MentorSkills.cs b/MBS_COMMAND.Domain/Entities/MentorSkills.cs index 02ddc58..5f7de3f 100644 --- a/MBS_COMMAND.Domain/Entities/MentorSkills.cs +++ b/MBS_COMMAND.Domain/Entities/MentorSkills.cs @@ -1,5 +1,3 @@ -using MBS_AUTHORIZATION.Domain.Entities; -using MBS_COMMAND.Contract.Services.MentorSkills; using MBS_COMMAND.Domain.Abstractions.Aggregates; using MBS_COMMAND.Domain.Abstractions.Entities; using DomainEventShared = MBS_CONTRACT.SHARE.Services.Mentors.DomainEvent; diff --git a/MBS_COMMAND.Domain/Entities/Schedule.cs b/MBS_COMMAND.Domain/Entities/Schedule.cs index 8d2f00b..be1bd1f 100644 --- a/MBS_COMMAND.Domain/Entities/Schedule.cs +++ b/MBS_COMMAND.Domain/Entities/Schedule.cs @@ -1,5 +1,4 @@ -using MBS_AUTHORIZATION.Domain.Entities; -using MBS_COMMAND.Domain.Abstractions.Entities; +using MBS_COMMAND.Domain.Abstractions.Entities; namespace MBS_COMMAND.Domain.Entities; diff --git a/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj b/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj index dc325c7..bc05a00 100644 --- a/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj +++ b/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj @@ -16,6 +16,7 @@ +