.NET7 中使用MailKit
MailKit 正式替换了.NET 的 SmtpClient
可参考:SmtpClient 类 (System.Net.Mail) | Microsoft Learn
static void SendMail(string subject, string html) { string userName = "邮箱"; string password = "密码"; string smtpHost = "smtp服务器地址"; int smtpPort = smtp服务器端口; bool smtpUseSSL = false; var message = new MimeMessage(); message.From.Add(new MailboxAddress(userName , userName )); message.To.Add(new MailboxAddress("收件人邮箱", "收件人邮箱")); message.Subject = subject; message.Body = new TextPart("html") { Text = html }; //message.Attachments. using (var client = new SmtpClient()) { client.Connect(smtpHost, smtpPort, smtpUseSSL); // Note: only needed if the SMTP server requires authentication client.Authenticate(userName, password); string result = client.Send(message); client.Disconnect(true); } }