Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add email sender #12

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading