transitition to pc

This commit is contained in:
2025-10-02 17:03:36 +04:00
parent d7f59ea747
commit ac62427ce7
2 changed files with 42 additions and 43 deletions

View File

@@ -3,46 +3,43 @@ using MimeKit;
namespace YAPWebApplication.Infrastructure
{
namespace YAPWebApplication.Services
public interface IEmailService
{
public interface IEmailService
{
Task SendEmailAsync(string to, string subject, string body, Stream? attachment = null, string? fileName = null);
}
public class EmailService : IEmailService
{
private readonly IConfiguration _config;
public EmailService(IConfiguration config)
{
_config = config;
}
public async Task SendEmailAsync(string to, string subject, string body, Stream? attachment = null, string? fileName = null)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("Shop Reports", _config["Email:SmtpUser"]));
emailMessage.To.Add(MailboxAddress.Parse(to));
emailMessage.Subject = subject;
var builder = new BodyBuilder { HtmlBody = body };
if (attachment != null && fileName != null)
{
attachment.Seek(0, SeekOrigin.Begin);
builder.Attachments.Add(fileName, attachment);
}
emailMessage.Body = builder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(_config["Email:SmtpServer"], int.Parse(_config["Email:SmtpPort"]), true);
await client.AuthenticateAsync(_config["Email:SmtpUser"], _config["Email:SmtpPass"]);
await client.SendAsync(emailMessage);
await client.DisconnectAsync(true);
}
}
Task SendEmailAsync(string to, string subject, string body, Stream? attachment = null, string? fileName = null);
}
public class EmailService : IEmailService
{
private readonly IConfiguration _config;
public EmailService(IConfiguration config)
{
_config = config;
}
public async Task SendEmailAsync(string to, string subject, string body, Stream? attachment = null, string? fileName = null)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("Shop Reports", _config["Email:SmtpUser"]));
emailMessage.To.Add(MailboxAddress.Parse(to));
emailMessage.Subject = subject;
var builder = new BodyBuilder { HtmlBody = body };
if (attachment != null && fileName != null)
{
attachment.Seek(0, SeekOrigin.Begin);
builder.Attachments.Add(fileName, attachment);
}
emailMessage.Body = builder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(_config["Email:SmtpServer"], int.Parse(_config["Email:SmtpPort"]), true);
await client.AuthenticateAsync(_config["Email:SmtpUser"], _config["Email:SmtpPass"]);
await client.SendAsync(emailMessage);
await client.DisconnectAsync(true);
}
}
}

View File

@@ -26,9 +26,11 @@
"ConnectionString": "Host=127.0.0.1;Port=5432;Database=YAP;Username=postgres;Password=postgres;"
},
"Email": {
"SmtpServer": "smtp.gmail.com",
"SmtpPort": "465",
"SmtpUser": "yourmail@gmail.com",
"SmtpPass": "app_password"
"SmtpServer": "smtp.yandex.ru",
"SmtpPort": 465,
"SmtpUser": "saratov.escaper@yandex.ru",
"SmtpPass": "gldtzvjtnfgzhhpu",
"FromName": "YAP Mail Service",
"FromEmail": "saratov.escaper@yandex.ru"
}
}