首页 > 其他分享 >.NET7 中使用MailKit

.NET7 中使用MailKit

时间:2023-02-01 02:33:05浏览次数:56  
标签:userName MailKit string SmtpClient client 使用 NET7 new message

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);
            }
        }

 

标签:userName,MailKit,string,SmtpClient,client,使用,NET7,new,message
From: https://www.cnblogs.com/ChenRihe/p/17081310.html

相关文章