42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Net;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Security.Cryptography;
|
|
using SimpleWiki.Settings;
|
|
|
|
namespace SimpleWiki.Services;
|
|
|
|
public static class EmailService
|
|
{
|
|
public static string SendPasswordResetCode(string toEmail)
|
|
{
|
|
if (!SmtpSettings.IsConfigured)
|
|
{
|
|
throw new InvalidOperationException("SMTP не настроен. Заполни Settings/SmtpSettings.cs.");
|
|
}
|
|
|
|
var code = RandomNumberGenerator.GetInt32(100000, 999999).ToString();
|
|
|
|
using var client = new SmtpClient(SmtpSettings.Host, SmtpSettings.Port)
|
|
{
|
|
EnableSsl = SmtpSettings.EnableSsl,
|
|
Credentials = new NetworkCredential(SmtpSettings.Email, SmtpSettings.Password)
|
|
};
|
|
|
|
using var message = new MailMessage
|
|
{
|
|
From = new MailAddress(SmtpSettings.Email, SmtpSettings.DisplayName),
|
|
Subject = "Код подтверждения для смены пароля",
|
|
Body = $"<h1 style='font-family:Segoe UI'>Код подтверждения: {code}</h1><p>Если это были не вы, просто проигнорируйте письмо.</p>",
|
|
IsBodyHtml = true,
|
|
BodyEncoding = Encoding.UTF8,
|
|
SubjectEncoding = Encoding.UTF8
|
|
};
|
|
|
|
message.To.Add(toEmail);
|
|
client.Send(message);
|
|
|
|
return code;
|
|
}
|
|
}
|