-
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 #12 from Tohirjon-Odilov/samurai/update-source
add email sender
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
Exam.StockManagement.Application/Abstractions/IServices/IEmailSenderService.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,7 @@ | ||
namespace Exam.StockManagement.Application.Abstractions.IServices | ||
{ | ||
public interface IEmailSenderService | ||
{ | ||
public Task<string> SendEmailAsync(string email, string path); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Exam.StockManagement.Application/Services/EmailSenderService.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,47 @@ | ||
using Exam.StockManagement.Application.Abstractions.IServices; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Net; | ||
using System.Net.Mail; | ||
|
||
namespace Exam.StockManagement.Application.Services | ||
{ | ||
public class EmailSenderService : IEmailSenderService | ||
{ | ||
private IConfiguration _config; | ||
|
||
public EmailSenderService(IConfiguration config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
public async Task<string> SendEmailAsync(string email, string path) | ||
{ | ||
var code = new Random().Next(1000, 9999).ToString(); | ||
|
||
IConfigurationSection? emailSettings = _config.GetSection("EmailSettings"); | ||
MailMessage? mailMessage = new MailMessage | ||
{ | ||
From = new MailAddress(emailSettings["Sender"]!, emailSettings["SenderName"]), | ||
Subject = "Ro'yhatdan o'tishingiz uchun parol.", | ||
Body = $"Sizning kodingiz: {code}", | ||
IsBodyHtml = true, | ||
|
||
}; | ||
mailMessage.To.Add(email!); | ||
|
||
using var smtpClient = new SmtpClient(emailSettings["MailServer"], int.Parse(emailSettings["MailPort"]!)) | ||
{ | ||
Port = Convert.ToInt32(emailSettings["MailPort"]), | ||
DeliveryMethod = SmtpDeliveryMethod.Network, | ||
Credentials = new NetworkCredential(emailSettings["Sender"], emailSettings["Password"]), | ||
EnableSsl = true, | ||
}; | ||
|
||
await File.WriteAllTextAsync(path, code); | ||
|
||
//smtpClient.UseDefaultCredentials = true; | ||
await smtpClient.SendMailAsync(mailMessage); | ||
return code; | ||
} | ||
} | ||
} |