/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailTo">要发送的邮箱</param>
/// <param name="mailSubject">邮箱主题</param>
/// <param name="mailContent">邮箱内容</param>
public static bool SendEmails(string mailTo, string mailSubject, string mailContent)
{
// 设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.qq.com"; //企业账号用smtp.exmail.qq.com
string mailFrom = "你的QQ号@qq.com"; //登陆用户名
string userPassword = "****";//这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码 // 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码 // 发送邮件设置
MailAddress maFrom = new MailAddress(mailFrom, "测试邮箱");//更改发件人标题
MailAddress maTo = new MailAddress(mailTo);
MailMessage mailMessage = new MailMessage(maFrom, maTo);
//MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
//mailMessage.CC.Add(mailTos);
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.High;//优先级
string[] file = { "文件路径" };
//添加邮件附件,可发送多个文件
foreach (var filename in file)
{
mailMessage.Attachments.Add(new Attachment(filename, MediaTypeNames.Application.Octet));
} try
{
smtpClient.Send(mailMessage); // 发送邮件
return true;
}
catch (SmtpException ex)
{
return false;
}
}
QQ邮箱的授权码开启方法
https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html