以163邮箱为例,借助MimeKit
nuget安装:MimeKit类库,源码地址:https://github.com/jstedfast/MimeKit
发送方法如下:
#region 邮件发送 /// <summary> /// 邮件发送 /// </summary> /// <param name="sendName">发送者名称</param> /// <param name="sendAccountName">发送者账号</param> /// <param name="smtpHost">发送者服务器地址:例如:smtp.163.com</param> /// <param name="smtpPort">服务器端口号:例如:25</param> /// <param name="authenticatePassword">发送者登录邮箱账号的客户端授权码</param> /// <param name="receiverAccountNameList">接收者账号</param> /// <param name="mailSubject">邮件主题</param> /// <param name="sendHtml">文本html(与sendText参数互斥,传此值则 sendText传null)</param> /// <param name="sendText">纯文本(与sendHtml参数互斥,传此值则 sendHtml传null)</param> /// <param name="accessoryList">邮件的附件</param> public static void SendMail(string sendName, string sendAccountName, string smtpHost, int smtpPort, string authenticatePassword, List<string> receiverAccountNameList, string mailSubject, string sendHtml, string sendText, List<MimeKit.MimePart> accessoryList = null) { var message = new MimeKit.MimeMessage(); message.From.Add(new MimeKit.MailboxAddress(sendName, sendAccountName)); var mailboxAddressList = new List<MailboxAddress>(); receiverAccountNameList.ForEach(f => { mailboxAddressList.Add(new MailboxAddress(f)); }); message.To.AddRange(mailboxAddressList); message.Subject = mailSubject; var alternative = new MimeKit.Multipart("alternative"); if (!string.IsNullOrWhiteSpace(sendText)) { alternative.Add(new MimeKit.TextPart("plain") { Text = sendText }); } if (!string.IsNullOrWhiteSpace(sendHtml)) { alternative.Add(new MimeKit.TextPart("html") { Text = sendHtml }); } var multipart = new MimeKit.Multipart("mixed"); multipart.Add(alternative); if (accessoryList != null) { accessoryList?.ForEach(f => { multipart.Add(f); }); } message.Body = multipart; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(smtpHost, smtpPort, false); client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(sendAccountName, authenticatePassword); client.Send(message); client.Disconnect(true); } } #endregion
使用方法:
SendMail("发送人名称", "xxxxxxx@163.com", "smtp.163.com", 25, "xxxxxxxxx授权码", new List<string> { { "xxxxxx@qq.com" } }, "邮件主题", @" <p>邮件文本</p> ", null, accessoryList: new List<MimePart>() { { new MimeKit.MimePart("audio","mp4") { ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\Users\lyj\Desktop\图片\下载 (5).mp4"), MimeKit.ContentEncoding.Default), ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment), ContentTransferEncoding = MimeKit.ContentEncoding.Base64, FileName = Path.GetFileName("C:\Users\lyj\Desktop\图片\下载 (5).mp4") } }, { new MimeKit.MimePart("audio","mp4") { ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\Users\lyj\Desktop\图片\下载 (3).mp4"), MimeKit.ContentEncoding.Default), ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment), ContentTransferEncoding = MimeKit.ContentEncoding.Base64, FileName = Path.GetFileName("C:\Users\lyj\Desktop\图片\下载 (3).mp4") } }, { new MimeKit.MimePart("image","jpg") { ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\Users\lyj\Desktop\图片\11timg.jpg"), MimeKit.ContentEncoding.Default), ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment), ContentTransferEncoding = MimeKit.ContentEncoding.Base64, FileName = Path.GetFileName("C:\Users\lyj\Desktop\图片\11timg.jpg") } } });
收录一些邮件服务地址对应的端口:https://blog.csdn.net/zdqdj1/article/details/48030023