mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-31 10:47:31 +08:00
* feat: add email and websocket notification * fix typos. * refactor(notifiers): 重构邮件和 WebSocket 通知器 - 提升 EmailNotifier 中的 SmtpClient 为类的成员变量,并在构造函数中初始化,优化资源使用 - 改进 WebSocketNotifier 的连接和重连逻辑,提高稳定性 - 优化通知器的错误处理和日志记录,增强可维护性 * Create BarkNotifier.cs * Update NotificationConfig.cs * Update NotificationService.cs * Update CommonSettingsPageViewModel.cs * Add files via upload * Add files via upload * Add files via upload * Update CommonSettingsPage.xaml * Delete BetterGenshinImpact/Service/Notifier/BarkNotifier.cs * Add files via upload * Update BarkNotifier.cs * Update NotificationService.cs * Update CommonSettingsPageViewModel.cs * Update CommonSettingsPage.xaml * Delete BetterGenshinImpact/Service/BarkNotifier.cs * Add files via upload * Add files via upload * Add files via upload * Update CommonSettingsPage.xaml * fix: 回退部分代码,修复了程序崩溃的错误 * fix: remove api.day.app --------- Co-authored-by: 秋云 <physligl@gmail.com> Co-authored-by: DR-lin-eng <52230594+DR-lin-eng@users.noreply.github.com>
111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using BetterGenshinImpact.Service.Notification.Model;
|
|
using BetterGenshinImpact.Service.Notifier.Exception;
|
|
using BetterGenshinImpact.Service.Notifier.Interface;
|
|
|
|
namespace BetterGenshinImpact.Service.Notifier
|
|
{
|
|
public class EmailNotifier : INotifier
|
|
{
|
|
public string Name { get; set; } = "Email";
|
|
|
|
// SMTP服务器配置
|
|
private readonly string _smtpServer;
|
|
private readonly int _smtpPort;
|
|
private readonly string _smtpUsername;
|
|
private readonly string _smtpPassword;
|
|
|
|
// 发件人配置
|
|
private readonly string _fromEmail;
|
|
private readonly string _fromName;
|
|
|
|
// 收件人邮箱
|
|
public string ToEmail { get; set; }
|
|
|
|
// 提升 SmtpClient 为类的成员变量
|
|
private readonly SmtpClient _smtpClient;
|
|
|
|
public EmailNotifier(
|
|
string smtpServer,
|
|
int smtpPort,
|
|
string smtpUsername,
|
|
string smtpPassword,
|
|
string fromEmail,
|
|
string fromName,
|
|
string toEmail = "")
|
|
{
|
|
_smtpServer = smtpServer;
|
|
_smtpPort = smtpPort;
|
|
_smtpUsername = smtpUsername;
|
|
_smtpPassword = smtpPassword;
|
|
_fromEmail = fromEmail;
|
|
_fromName = fromName;
|
|
ToEmail = toEmail;
|
|
|
|
// 在构造函数中初始化 SmtpClient
|
|
_smtpClient = new SmtpClient(_smtpServer, _smtpPort)
|
|
{
|
|
Credentials = new System.Net.NetworkCredential(_smtpUsername, _smtpPassword),
|
|
EnableSsl = true
|
|
};
|
|
}
|
|
|
|
public async Task SendAsync(BaseNotificationData content)
|
|
{
|
|
if (string.IsNullOrEmpty(ToEmail))
|
|
{
|
|
throw new NotifierException("收件人邮箱地址为空");
|
|
}
|
|
|
|
try
|
|
{
|
|
using var mailMessage = new MailMessage
|
|
{
|
|
From = new MailAddress(_fromEmail, _fromName),
|
|
Subject = FormatEmailSubject(content),
|
|
Body = FormatEmailBody(content),
|
|
IsBodyHtml = true
|
|
};
|
|
|
|
mailMessage.To.Add(ToEmail);
|
|
|
|
// 使用成员变量 _smtpClient 发送邮件
|
|
await _smtpClient.SendMailAsync(mailMessage);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
throw new NotifierException($"发送邮件失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private string FormatEmailSubject(BaseNotificationData content)
|
|
{
|
|
// 可以根据实际需求自定义邮件主题格式
|
|
return $"通知 - {content.GetType().Name}";
|
|
}
|
|
|
|
private string FormatEmailBody(BaseNotificationData content)
|
|
{
|
|
var builder = new StringBuilder();
|
|
builder.AppendLine("<html><body>");
|
|
|
|
// 添加通知标题
|
|
builder.AppendLine("<h2>通知详情</h2>");
|
|
|
|
// 添加通知内容
|
|
foreach (var prop in content.GetType().GetProperties())
|
|
{
|
|
var value = prop.GetValue(content);
|
|
if (value != null)
|
|
{
|
|
builder.AppendLine($"<p><strong>{prop.Name}:</strong> {value}</p>");
|
|
}
|
|
}
|
|
|
|
builder.AppendLine("</body></html>");
|
|
return builder.ToString();
|
|
}
|
|
}
|
|
} |