1,开启服务,获取授权码。以QQ邮箱为例:
点击管理服务,进入账号与安全页面
2,相关设置参数,以QQ邮箱为例:
登录时,请在第三方客户端的密码输入框里面填入授权码进行验证。(不是填入QQ的密码)
IMAP/SMTP 设置方法
用户名/帐户: 你的QQ邮箱完整的地址
密码: 生成的授权码
电子邮件地址: 你的QQ邮箱的完整邮件地址
接收邮件服务器: imap.qq.com,使用SSL,端口号993
发送邮件服务器: smtp.qq.com,使用SSL,端口号587
POP3/SMTP 设置方法
用户名/帐户: 你的QQ邮箱完整的地址
密码: 生成的授权码
电子邮件地址: 你的QQ邮箱的完整邮件地址
接收邮件服务器: pop.qq.com,使用SSL,端口号995
发送邮件服务器: smtp.qq.com,使用SSL,端口号587
3,2次包装代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Common
{
/// <summary>
/// 软件的邮箱类,用于发送邮箱数据
/// </summary>
public class Emailhelp
{
public static Emailhelp MailSystem163 = new Emailhelp(
mail =>
{
mail.Host = "smtp.163.com";//使用163的SMTP服务器发送邮件
//mail.UseDefaultCredentials = true;//在winform平台使用默认值
mail.EnableSsl = true;
mail.UseDefaultCredentials = false;//在.framework或mvc下使用这个
mail.Port = 25;//端口号
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.Credentials = new System.Net.NetworkCredential("邮件发送地址(自己的邮件号)如1234656@qq.com", "刚才获取的授权码");
},
"邮件发送地址(自己的邮件号)如1234656@qq.com",
"邮件发送地址(自己的邮件号)如1234656@qq.com"
);
/// <summary>
/// 系统连续发送失败的次数,为了不影响系统,连续三次失败就禁止发送
/// </summary>
private static long SoftMailSendFailedCount { get; set; } = 0L;
/// <summary>
/// 系统的邮件发送客户端
/// </summary>
private SmtpClient smtpClient { get; set; }
/// <summary>
/// 发送邮件的地址
/// </summary>
private string MailFromAddress { get; set; } = "";
/// <summary>
/// 邮件发送的地址
/// </summary>
public string MailSendAddress { get; set; } = "";
/// <summary>
/// 实例化一个邮箱发送类,需要指定初始化信息
/// </summary>
/// <param name="mailIni">初始化的方法</param>
/// <param name="addr_From">发送地址,应该和账户匹配</param>
/// <param name="addr_to">邮件接收地址</param>
/// <remarks>
/// </remarks>
public Emailhelp(Action<SmtpClient> mailIni, string addr_From = "", string addr_to = "")
{
smtpClient = new SmtpClient();
mailIni(smtpClient);
MailFromAddress = addr_From;
MailSendAddress = addr_to;
}
private string GetExceptionMail(Exception ex)
{
return ex.Message;
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="addr_from">发送地址</param>
/// <param name="name">发送别名</param>
/// <param name="addr_to">接收地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="attachment">附件地址</param>
/// <param name="isHtml">邮件内容是否是HTML语言</param>
/// <param name="priority">优先级</param>
/// <returns>发生是否成功,内容不正确会被视为垃圾邮件</returns>
public bool SendMail(string addr_from, string name, string[] addr_to, string subject, string body, string[] attachment, bool isHtml = false, MailPriority priority = MailPriority.Normal)
{
if (SoftMailSendFailedCount > 10)
{
SoftMailSendFailedCount++;
return true;
}
MailMessage mailMessage = new MailMessage();
try
{
mailMessage.From = new MailAddress(addr_from, name, Encoding.UTF8);
foreach (string addresses in addr_to)
{
mailMessage.To.Add(addresses);
}
mailMessage.Subject = subject;
mailMessage.Body = body;
MailMessage mailMessage2 = mailMessage;
mailMessage2.Body = mailMessage2.Body + Environment.NewLine + Environment.NewLine + Environment.NewLine;
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.Priority = priority;
mailMessage.IsBodyHtml = isHtml;
for (int i = 0; i < attachment.Length; i++)
{
Attachment address = new Attachment(attachment[i]);
mailMessage.Attachments.Add(address);
}
smtpClient.Send(mailMessage);
SoftMailSendFailedCount = 0L;
return true;
}
catch (Exception ex)
{
SoftMailSendFailedCount++;
return false;
}
}
/// <summary>
/// 发送内容带有图片的邮件
/// </summary>
/// <param name="sfrom">发件人邮箱地址</param>
/// <param name="displayName">显示名</param>
/// <param name="addr_to">收件人地址</param>
/// <param name="sSubject">标题</param>
/// <param name="sBody">邮件内容</param>
/// <param name="attachment">邮件附件</param>
/// <returns></returns>
public bool Send(string sfrom, string displayName, string[] addr_to, string sSubject, string sBody, string[] attachment)
{
MailMessage oMail = new MailMessage();
oMail.From = new MailAddress(sfrom, displayName, Encoding.UTF8);
foreach (var item in addr_to)
{
oMail.To.Add(item);
}
// 添加附件
if (attachment != null)
{
foreach (var item in attachment)
{
oMail.Attachments.Add(new Attachment(item));
}
}
sBody = ReplaceImg(sBody, oMail); // 处理图片
oMail.Subject = sSubject; //邮件标题
oMail.Body = sBody; //邮件内容
oMail.IsBodyHtml = true; //邮件格式
oMail.BodyEncoding = Encoding.GetEncoding("GB2312"); //邮件采用的编码
oMail.Priority = MailPriority.High; //设置邮件的优先级为高
try
{
smtpClient.Send(oMail);
return true;
}
catch (Exception e)
{
SoftMailSendFailedCount++;
return false;
}
finally
{
oMail.Dispose(); //释放资源
}
}
/// <summary>
/// 处理邮件内容中的图片
/// 将图片改为附件形式在body中显示
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
private string ReplaceImg(string body, MailMessage m)
{
try
{
Dictionary<string, Stream> imgDic = new Dictionary<string, Stream>();
body = GetImgStream(body, imgDic);
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
if (imgDic.Count != 0) // 内容添加图片
{
foreach (var dic in imgDic)
{
LinkedResource lrImage = new LinkedResource(dic.Value, "image/gif");
lrImage.ContentId = dic.Key;
avHtml.LinkedResources.Add(lrImage);
}
}
m.AlternateViews.Add(avHtml);
return body;
}
catch (Exception)
{
return body;
}
}
private string GetImgStream(string body, Dictionary<string, Stream> imgDic)
{
string mactch;
Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc\s*=\s*(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
MatchCollection mc = reg.Matches(body);
for (int i = 0; i < mc.Count; i++)
{
string key = "pic" + i;
mactch = mc[i].Groups["src"].Value;
body = body.Replace(mactch, "cid:" + key);
FileStream fs = new FileStream(mactch, FileMode.Open);
imgDic.Add(key, fs);
}
return body;
}
}
}
4,邮件效果:
特别说明:若要实现:
第1,正文带有背景图片。
第2,正文为自定义编辑风格(带有边框,不同颜色的字体)。
正文需要为html格式,普通文本格式无法实现。