/// <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; } }
感謝作者分享
https://blog.csdn.net/qq_21275565/article/details/125422780
https://pythonjishu.com/ldzbkuvvhvzqcdi/
标签:string,C#,发送,附件,client,new,msg,邮箱,邮件 From: https://www.cnblogs.com/ViolinHuang/p/17622309.html