1、进入qq邮箱首页,点击设置
2、点击 账户 选项卡 ,划到大概中间部分,有一个服务选项区。经实验开启前两个就可以了。
3、点击下方的 生成授权码,在开启服务之后会自动生成一次。这一步如果绑定了密保,会先进行一次身份验证。验证之后会发放一个授权码。这个授权码,在程序里会用到。
*******************************************************代码部分*******************************************************
public class EmailService : IEmailService
{ private readonly IConfiguration _configuration; public EmailService(IConfiguration configuration) { _configuration = configuration; } public async Task SendEmailAsync(MimeMessage message) { var host = "smtp.qq.com"; var port = 465; var useSsl = true; var from_username = "*******@qq.com"; // 授权码 var from_password = "********"; var from_name = "好医生"; var from_address = "********@qq.com"; var address = new List<MailboxAddress> { new MailboxAddress("123456","[email protected]"), new MailboxAddress("56789","[email protected]") }; message.From.Add(new MailboxAddress(from_name, from_address)); message.To.AddRange(address); using var client = new SmtpClient { ServerCertificateValidationCallback = (s, c, h, e) => true }; client.AuthenticationMechanisms.Remove("XOAUTH2"); await client.ConnectAsync(host, port, useSsl); await client.AuthenticateAsync(from_username, from_password); await client.SendAsync(message); await client.DisconnectAsync(true); } }
调用
var message = new MimeMessage { Subject = "程序异常", Body = new BodyBuilder { HtmlBody = msg }.ToMessageBody() }; await _emailService.SendEmailAsync(message);
标签:qq,core,client,var,new,net,com,message From: https://www.cnblogs.com/yxzs/p/16661870.html