C#发送邮件

发布时间 2024-01-08 11:34:57作者: 亦承
  /// <summary>
        /// 加载邮件模板
        /// </summary>
        /// <param name="TempPath">C:\Users\Administrator\Documents\模板.html</param>
        /// <param name="Params">模板参数</param>
        /// <returns></returns>
        public static string GetEmailHtml(string TempPath, string[] Params)
        {
            string BodyHtml = "";
            #region 读取模板内容,拼接邮件内容

            using (System.IO.StreamReader sr = new System.IO.StreamReader(TempPath, Encoding.UTF8, true))
            {
                BodyHtml = sr.ReadToEnd();
            }
            //拼接邮件内容
            return string.Format(BodyHtml, Params);
            #endregion
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="Subject">主题</param>
        /// <param name="Body">内容</param>
        /// <param name="DisplayName">描述:可以填写公司名称</param>
        /// <param name="ToEmails">接受邮箱</param>
        /// <param name="BCC">抄送邮箱</param>
        /// <param name="FilePaths">附件</param>
        public static void SendEamil(string Subject, string Body, string DisplayName, string[] ToEmails, string[] BCC, string[] FilePaths, bool IsBodyHtml = true)
        {
            string host = ConfigurationManager.AppSettings["email_host"];//smtp.qq.com QQ邮箱的
            string port = ConfigurationManager.AppSettings["email_port"];//端口号 QQ邮箱使用587
            string email_fromEmail = ConfigurationManager.AppSettings["email_fromEmail"];//发送的邮箱号
            string email_shouQuanMa = ConfigurationManager.AppSettings["email_shouQuanMa"];////授权码需要在 邮箱里面  设置-账户 -POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务那边获取
            try
            {

                using (var client = new SmtpClient(host, Convert.ToInt32(port)))
                using (var mailMessage = new MailMessage())
                {
                    client.UseDefaultCredentials = false;
                    //dasavnftbbaksibhjj
                    client.Credentials = new NetworkCredential(email_fromEmail, email_shouQuanMa);
                    //发送人
                    mailMessage.From = new MailAddress(email_fromEmail, DisplayName);
                    //接收人
                    foreach (string email in ToEmails)
                    {
                        mailMessage.To.Add(new MailAddress(email));
                    }
                    //抄送人
                    if (BCC != null)
                    {
                        foreach (string email in BCC)
                        {
                            mailMessage.Bcc.Add(new MailAddress(email));
                        }
                    }

                    mailMessage.Body = Body;
                    mailMessage.IsBodyHtml = IsBodyHtml;
                    mailMessage.Subject = Subject;
                    //附件内容
                    foreach (string FilePath in FilePaths)
                    {
                        Attachment attachment1 = new Attachment(FilePath);//默认文件名称就是附件名称
                        mailMessage.Attachments.Add(attachment1);
                    }

                    //Stream stream = File.OpenRead(@"C:\Users\Administrator\Documents\发布流程(1).pdf");//这个是需要填写附件名称得
                    //附件内容
                    //Attachment attachment2 = new Attachment(stream, "2.pdf");
                    //mailMessage.Attachments.Add(attachment2);
                    SaveLog($"发送邮件!!ToEmails:{string.Join(",", ToEmails)}", 0, "error");
                    client.SendMailAsync(mailMessage).GetAwaiter().GetResult();
                }
            }
            catch (Exception ex)
            {
                SaveLog($"发送邮件失败了!!host:{host},port:{port},email_fromEmail:{email_fromEmail},email_shouQuanMa:{email_shouQuanMa},ToEmails:{string.Join(",", ToEmails)},FilePaths:{string.Join(",", FilePaths)}详细信息::" + ex, 0, "error");
            }
        }