分享

ASP.NET 邮件群发以及将数据库的数据以table形式发送

 行走在理想边缘 2019-03-24

代码写在Global.aszx中,系统自动运行

1.Application_Start()中设定一个定时器以及邮件发送事件

  1. protected void Application_Start(object sender, EventArgs e)
  2. {
  3. System.Timers.Timer timer = new System.Timers.Timer(30000);
  4. timer.Elapsed += new System.Timers.ElapsedEventHandler(Send);
  5. timer.Start();
  6. }

2.对Send()方法进行编辑,设定发送的时间、发送邮箱和接收邮箱

  1. public void Send(object sender, System.Timers.ElapsedEventArgs e)
  2. {
  3. SqlConnection myconn = new SqlConnection("Data Source=100.0.4.51;Initial Catalog=XRX;User ID=xrx;Password=b879d~c2a81fd#9b8e33@ffd5!85e5c6cfb9");
  4. string sql = "select mail from MailTest";
  5. SqlCommand mycmd = new SqlCommand(sql, myconn);
  6. if (myconn.State == ConnectionState.Closed)
  7. {
  8. myconn.Open();
  9. }
  10. SqlDataReader reader = mycmd.ExecuteReader();


  11. SqlConnection myconn2 = new SqlConnection("Data Source=100.0.4.51;Initial Catalog=XRX;User ID=xrx;Password=b879d~c2a81fd#9b8e33@ffd5!85e5c6cfb9");
  12. myconn2.Open();
  13. string sql2 = "select Student.StuID,Student.StuName,Course.CourseName,StudentSign.SignState,StudentSign.OutState, StudentSign.LeaveState,StudentSign.ReMark,StudentSign.LateState from StudentSign join Schedule on Schedule.ScheduleID = StudentSign.ScheduleID join Student on Student.ID = StudentSign.StuID join Course on Course.ID = Schedule.CourseID group by Student.StuID,Student.StuName,Course.CourseName,StudentSign.SignState, StudentSign.OutState,StudentSign.LeaveState,StudentSign.ReMark,StudentSign.LateState";
  14. SqlCommand mycmd2 = new SqlCommand(sql2, myconn2);
  15. SqlDataReader reader2 = mycmd2.ExecuteReader();
  16. List<string> record = new List<string>();
  17. while (reader2.Read())
  18. {
  19. string msg = "<td>"+reader2["StuID"].ToString() + "</td><td>" + reader2["StuName"].ToString() + "</td><td>" + reader2["CourseName"].ToString() + "</td><td>" + reader2["SignState"].ToString() + "</td><td>" + reader2["OutState"].ToString() + "</td><td>" + reader2["LeaveState"].ToString() + "</td><td>" + reader2["Remark"].ToString() + "</td><td>" + reader2["LateState"].ToString() + "</td>";
  20. record.Add(msg);
  21. }
  22. myconn2.Close();


  23. if (DateTime.Now.Minute == 10)
  24. {
  25. string mailBody = getMailBody(record);
  26. while (reader.Read())
  27. {
  28. string aMail = reader["mail"].ToString();
  29. //string msg = reader2["StuID"].ToString() +" "+ reader2["StuName"].ToString() +" "+ reader2["CourseName"].ToString() + " " + reader2["SignState"].ToString() + " " + reader2["OutState"].ToString();
  30. SendEMail("'" + aMail + "'", "'" + aMail + "'", "考勤信息统计", "" + mailBody + "");
  31. }
  32. //endEMail sm = new SendMail();
  33. //SendEMail("'"+sql+"'", "'" + sql + "'", "Auto Mail", "This is a auto amil!");
  34. }
  35. myconn.Close();
  36. }
1.筛选出需要接收邮件的邮箱地址
SqlConnection myconn = new SqlConnection("Data Source=100.0.4.51;Initial Catalog=XRX;User ID=xrx;Password=b879d~c2a81fd#9b8e33@ffd5!85e5c6cfb9");          
            string sql = "select mail from MailTest";
            SqlCommand mycmd = new SqlCommand(sql, myconn);
            if (myconn.State == ConnectionState.Closed)
            {
                myconn.Open();
            }
            SqlDataReader reader = mycmd.ExecuteReader();

2.筛选出所需要的信息,创建链表,循环读取并储存在链表中,将字段数据以table的形式保存
SqlConnection myconn2 = new SqlConnection("Data Source=100.0.4.51;Initial Catalog=XRX;User ID=xrx;Password=b879d~c2a81fd#9b8e33@ffd5!85e5c6cfb9");
            myconn2.Open();
            string sql2 = "select Student.StuID,Student.StuName,Course.CourseName,StudentSign.SignState,StudentSign.OutState, StudentSign.LeaveState,StudentSign.ReMark,StudentSign.LateState from StudentSign join Schedule on Schedule.ScheduleID = StudentSign.ScheduleID join Student on Student.ID = StudentSign.StuID join Course on Course.ID = Schedule.CourseID group by Student.StuID,Student.StuName,Course.CourseName,StudentSign.SignState, StudentSign.OutState,StudentSign.LeaveState,StudentSign.ReMark,StudentSign.LateState";
            SqlCommand mycmd2 = new SqlCommand(sql2, myconn2);                            
            SqlDataReader reader2 = mycmd2.ExecuteReader();
            List<string> record = new List<string>();
            while (reader2.Read())
            {
                string msg = "<td>"+reader2["StuID"].ToString() + "</td><td>" + reader2["StuName"].ToString() + "</td><td>" + reader2["CourseName"].ToString() + "</td><td>" + reader2["SignState"].ToString() + "</td><td>" + reader2["OutState"].ToString() + "</td><td>" + reader2["LeaveState"].ToString() + "</td><td>" + reader2["Remark"].ToString() + "</td><td>" + reader2["LateState"].ToString() + "</td>";
                record.Add(msg);
            }
            myconn2.Close();

3.生成读取邮件内容的方法,创建StringBuilder类

 private string getMailBody(List<String> list)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table><tr><td>学号</td><td>姓名</td><td>课程</td><td>签到状态</td><td>签退状态                                         </td><td>请假</td><td>标记</td><td>迟到</td></tr>");
            foreach (string aStr in list){                
                result.Append("<tr>").Append(aStr).Append("</tr>");
            }
            result.Append("</table>");
            return result.ToString();

        }

3.设定邮件发送时间,读取邮件地址和内容

  if (DateTime.Now.Minute == 10)
            {
                string mailBody = getMailBody(record);
                while (reader.Read())
                {
                    string aMail = reader["mail"].ToString();
                    SendEMail("'" + aMail + "'", "'" + aMail + "'", "考勤信息统计", "" + mailBody + "");
                }             
            }

            myconn.Close();    

DataTime.Now. xx  可以按照自己的需求去选择发送邮件的间隔

3.对SendEmail()方法进行编写

  1. public void SendEMail(string To1, string CC1, /*string BC1,*/ string Subject1, string Body1)
  2. {
  3. MailMessage msg = new MailMessage("xxxx@qq.com", To1);
  4. msg.CC.Add(CC1);
  5. //msg.Bcc.Add(BC1);
  6. msg.Subject = Subject1;
  7. msg.Body = Body1;
  8. msg.IsBodyHtml = true;
  9. msg.Priority = MailPriority.High;//发送邮件的优先等级
  10. SmtpClient c = new SmtpClient("smtp.qq.com", 587);
  11. System.Net.NetworkCredential basicAuthenticationInfo =
  12. new System.Net.NetworkCredential("xxxxx@qq.com", "xxxxxx");//用户名与SMTP授权码
  13. c.Credentials = basicAuthenticationInfo;
  14. c.EnableSsl = true;//启用SSL加密
  15. //c.Port = 587;
  16. c.Send(msg);
  17. }

4.新建一个web窗体,运行程序


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多