diff --git a/MBS-COMMAND.API/Program.cs b/MBS-COMMAND.API/Program.cs index e27b7d7..b00d82b 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.Development.json b/MBS-COMMAND.API/appsettings.Development.json index 368a0f6..e4789f2 100644 --- a/MBS-COMMAND.API/appsettings.Development.json +++ b/MBS-COMMAND.API/appsettings.Development.json @@ -1,7 +1,6 @@ { "ConnectionStrings": { - // "ConnectionStrings": "Server=103.162.14.116;Database=PRN_SUPER;Uid=sa;Pwd=MyStrongPassword123@;Trust Server Certificate=True;", - "ConnectionStrings": "Server=(local);Database=PRN_SUPER;Uid=sa;Pwd=12345;Trust Server Certificate=True;", + "ConnectionStrings": "Server=103.162.14.116;Database=PRN_SUPER;Uid=sa;Pwd=MyStrongPassword123@;Trust Server Certificate=True;", "Redis": "103.162.14.116:6379,password=MyStrongPassword123@,abortConnect=false" }, "JwtOption": { @@ -11,17 +10,24 @@ "ExpireMin": 5 }, "MasstransitConfiguration": { - "Host": "localhost", - "VHost": "phucnghi", + "Host": "103.162.14.116", + "VHost": "myHost", "Port": 5672, - "UserName": "guest", - "Password": "guest" + "UserName": "sa", + "Password": "MyStrongPassword123@" }, "MessageBusOptions": { "retryLimit": 3, "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..4ecb220 100644 --- a/MBS-COMMAND.API/appsettings.json +++ b/MBS-COMMAND.API/appsettings.json @@ -26,6 +26,13 @@ "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.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.cs index 4421d45..e12b24b 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.cs @@ -1,4 +1,3 @@ -using MBS_AUTHORIZATION.Domain.Entities; using MBS_COMMAND.Application.Abstractions; using MBS_COMMAND.Contract.Abstractions.Messages; using MBS_COMMAND.Contract.Abstractions.Shared; 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.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..bc05a00 100644 --- a/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj +++ b/MBS_COMMAND.Infrastucture/MBS_COMMAND.Infrastucture.csproj @@ -16,6 +16,7 @@ + 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