使用MailKit发送电子邮件
1. 通过NUGET包安装MailKit包
2. 代码示例
public static void SendMailByMailKit() { var message = new MimeKit.MimeMessage(); var bodyBuilder = new MimeKit.BodyBuilder(); // 发件人 message.From.Add(new MimeKit.MailboxAddress("from_name", "发件人邮箱地址")); // 收件人 message.To.Add(new MimeKit.MailboxAddress("to_name", "收件人邮箱地址")); // 快捷回复 message.ReplyTo.Add(new MimeKit.MailboxAddress("reply_name", "快捷回复人邮箱地址")); // 主题 message.Subject = "subject"; // 正文 bodyBuilder.HtmlBody = "html body"; // 附件 bodyBuilder.Attachments.Add("C:\\Users\\Administrator\\Desktop\\201912191443081749.xlsx"); message.Body = bodyBuilder.ToMessageBody(); var client = new MailKit.Net.Smtp.SmtpClient(); client.ServerCertificateValidationCallback = (s, c, h, e) => true; // 连接SMTP主机 client.Connect("smtp.exmail.qq.com", 25, MailKit.Security.SecureSocketOptions.Auto); client.Authenticate("userName", "password"); client.Send(message); client.Disconnect(true); }
3. 结果展示
发送的邮件结果: