- 前言
- 一、后端代码
- 二、HTML模板
前言
工作笔记:VS 2019 MailKit 发送邮件,
NuGet下载:MailKit,Mimekit
引用:
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
一、后端代码
public string sendMail(SendMailConfig config, SendMailEntity entity)
{
string receiver = entity.Receiver;
if (null == receiver || receiver.Trim().Equals(""))
{
throw new Exception("收件人不能为空");
}
string[] cc = (null == entity.CopyFor || entity.CopyFor.Equals("")) ? null : entity.CopyFor.Split(';');
string[] attach = (null == entity.Attach || entity.Attach.Equals("")) ? null : entity.Attach.Split(',');
sendMail(config.username, config.password, config.from, receiver.Split(';'), cc, null, entity.Title, entity.Content, attach, config);
return "ok";
}
///
/// 邮件发送
///
/// 用户名
/// 密码
/// 发件邮箱
/// 收件邮箱
/// 抄送
/// 密送
/// 标题
/// 内容
/// 附件
/// 配置
public void sendMail(string username, string password, string from, string[] to, string[] cc, string[] bcc, string subject, string content, string[] attachfilenames, SendMailConfig config)
{
using (var client = new SmtpClient())
{
var message = new MimeMessage();
#region from to cc bcc
if (string.IsNullOrWhiteSpace(from))
{
message.From.Add(new MailboxAddress(from, from));
}
else
{
message.From.Add(new MailboxAddress(config.fromdisplayname, config.from));
}
for (int i = 0; i < to.Length; i++)
{
if (!string.ReferenceEquals(to[i], null) && !to[i].Trim().Equals(""))
{
message.To.Add(new MailboxAddress(to[i], to[i]));
}
}
if (cc != null && !cc.Equals(""))
{
for (int i = 0; i < cc.Length; i++)
{
if (!string.ReferenceEquals(cc[i], null) && !cc[i].Trim().Equals(""))
{
message.Cc.Add(new MailboxAddress(cc[i], cc[i]));
}
}
}
if (bcc != null)
{
for (int i = 0; i < bcc.Length; i++)
{
if (!string.ReferenceEquals(bcc[i], null) && !bcc[i].Trim().Equals(""))
{
message.Bcc.Add(new MailboxAddress(bcc[i], bcc[i]));
}
}
}
#endregion
message.Subject = subject;
BodyBuilder _body = new BodyBuilder
{
HtmlBody = content,
};
if (attachfilenames != null && attachfilenames.Length > 0)
{
for (int i = 0; i < attachfilenames.Length; i++)
{
if (!string.ReferenceEquals(attachfilenames[i], null))
{
string[] n = attachfilenames[i].Split('@');
if (n.Length >= 2)
{
_body.Attachments.Add(n[0], obs.downFile(n[1]));
}
}
}
}
message.Body = _body.ToMessageBody();
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
if (config.smtp.starttlsEnable && !config.smtp.sslEnable) //config.getSmtp.sslEnable 端口不再是默认端口 25 而是465
{
client.Connect(config.smtp.host, config.smtp.port, SecureSocketOptions.StartTls);
}
else
{
client.Connect(config.smtp.host, config.smtp.port, config.smtp.sslEnable);
}
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(username, password);
client.Send(message);
client.Disconnect(true);
}
}
///
/// 获取HTML模板 我的模板是配置在目录下的,通过路径拿到文件内的内容
///
///
///
public string BuildByFile(string filePath)
{
StreamReader reader = null;
string template = string.Empty;
reader = new StreamReader(filePath);
template = reader.ReadToEnd();
reader.Close();
return template;
}
二、HTML模板
{-- 最上边紫色的分割线 --}} -->
{-- 页头的LOGO --}} -->
HI:
请复制下方链接至浏览器打开:
邮件来自XXX,不需要回复。



