/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sendEmail">收件人邮箱</param>
/// <param name="fromEmail">发件人邮箱</param>
/// <param name="fromName">发件人名称</param>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="sendCC">抄送人邮箱</param>
public static void SendEmail(string sendEmail, string fromEmail, string fromName, string title, string body, List<string> sendCC)
{MailMessage msg = new MailMessage();msg.To.Add(sendEmail); //收件人邮箱msg.From = new MailAddress(fromEmail, fromName); //发件人邮箱和名称msg.Subject = title;//邮件标题 msg.SubjectEncoding = Encoding.UTF8; //邮件标题格式为UTF8 msg.Body = body;//邮件内容msg.BodyEncoding = Encoding.UTF8; //邮件内容格式为UTF8 msg.IsBodyHtml = true;//设置邮件格式为html格式//设置抄送人地址if (sendCC != null && sendCC.Count > 0){foreach (String cc in sendCC){msg.CC.Add(cc);}}//添加附件//string filePath = @"C:\附件.xls";//msg.Attachments.Add(new Attachment(filePath));SmtpClient client = new SmtpClient();client.Host = "smtp.qq.com"; //SMTP服务器地址 client.Port = 587; //SMTP端口,QQ邮箱填写587 client.EnableSsl = true; //启用SSL加密 (使用除QQ邮箱之外的最好关闭)client.Credentials = new System.Net.NetworkCredential(fromEmail, "xxxxxxxxxxx"); //发件人邮箱账号,授权码(不是密码)try{client.Send(msg); //发送邮件}catch (Exception){throw;}
}
注意:如果使用qq邮箱,密码请从邮箱中获取,如下图
设置 => 账号 => POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV