public static async Task<bool> SendMailAsync22(string Name, string receive, string sender, string password, string host, int port, string subject, string body) { try { //# MimeMessage代表一封电子邮件的对象 var message = new MimeMessage(); //# 添加发件人地址 Name 发件人名字 sender 发件人邮箱 message.From.Add(new MailboxAddress(Name, sender)); //# 添加收件人地址 message.To.Add(new MailboxAddress("", receive)); //# 设置邮件主题信息 message.Subject = subject; //# 设置邮件内容 var bodyBuilder = new BodyBuilder() { HtmlBody = body }; message.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; // Note: since we don‘t have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); client.CheckCertificateRevocation = false; //client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12; client.Connect(host, port, MailKit.Security.SecureSocketOptions.Auto); // Note: only needed if the SMTP server requires authentication client.Authenticate(sender, password); await client.SendAsync(message); client.Disconnect(true); return true; } } catch (Exception ex) { throw ex; } return false; }
注意这里的Host 和Port .有邮箱服务商提供。比如:"smtp.yeah.net", 25
这里的密码,不是邮箱的登录密码,而是登录邮箱后需要设置第三方的一个发送邮件密码。
这也可以理解。这个密码只是用来发邮件的。即使别人拿到这个密码,也只能向外发邮件,而不能查看邮件等。