using System.Net; using System.Net.Mail; using XFEExtension.NetCore.Exceptions; namespace XFEExtension.NetCore.MailExtension; /// /// XFE邮件 /// public class XFEMail { /// /// 发送者名称 /// public string? SenderName { get; set; } /// /// 对方邮箱地址 /// public string? ToAddress { get; set; } /// /// 密码 /// public string? Password { get; set; } /// /// 发送邮件 /// /// 标题 /// 内容 /// public void SendEmail(string subTitle, string content) { try { using var client = new SmtpClient("smtp.exmail.qq.com", 587); client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("mail@xfegzs.com", Password); client.EnableSsl = true; MailAddress fromMailAddress = new("mail@xfegzs.com", SenderName); MailMessage message = new() { From = fromMailAddress }; if (ToAddress is not null) message.To.Add(ToAddress); else throw new XFEMailException("地址不能为空!"); message.Subject = subTitle; message.Body = content; // 发送邮件 client.SendAsync(message, null); } catch (Exception ex) { throw new XFEMailException("邮件发送错误!" + ex.Message); } } /// /// XFE邮件系统 /// /// 名称 /// 目标地址 /// 密码 public XFEMail(string senderName, string toAddress, string password) { SenderName = senderName; ToAddress = toAddress; Password = password; } /// /// XFE邮件系统 /// /// 密码 public XFEMail(string password) { Password = password; } /// /// XFE邮件系统 /// public XFEMail() { } }