Skip to content

Commit

Permalink
Merge pull request #12 from Tohirjon-Odilov/samurai/update-source
Browse files Browse the repository at this point in the history
add email sender
  • Loading branch information
Tohirjon-Odilov authored Mar 3, 2024
2 parents 511f01b + 5fe47a8 commit 8ecf21d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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 Exam.StockManagement.Application/Services/EmailSenderService.cs
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;
}
}
}

0 comments on commit 8ecf21d

Please sign in to comment.