-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from bentran1vn/Config/MailService
Config/mail service
- Loading branch information
Showing
10 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
1 change: 0 additions & 1 deletion
1
MBS_COMMAND.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
MBS_COMMAND.Infrastucture/DependencyInjection/Options/MailOption.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MailOption> 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); | ||
} | ||
} |