日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

ASP.NET 郵件群發(fā)以及將數(shù)據(jù)庫的數(shù)據(jù)以table形式發(fā)送

 行走在理想邊緣 2019-03-24

代碼寫在Global.aszx中,系統(tǒng)自動(dòng)運(yùn)行

1.Application_Start()中設(shè)定一個(gè)定時(shí)器以及郵件發(fā)送事件

  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()方法進(jìn)行編輯,設(shè)定發(fā)送的時(shí)間、發(fā)送郵箱和接收郵箱

  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 + "'", "考勤信息統(tǒng)計(jì)", "" + 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.篩選出所需要的信息,創(chuàng)建鏈表,循環(huán)讀取并儲存在鏈表中,將字段數(shù)據(jù)以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.生成讀取郵件內(nèi)容的方法,創(chuàng)建StringBuilder類

 private string getMailBody(List<String> list)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table><tr><td>學(xué)號</td><td>姓名</td><td>課程</td><td>簽到狀態(tài)</td><td>簽退狀態(tài)                                         </td><td>請假</td><td>標(biāo)記</td><td>遲到</td></tr>");
            foreach (string aStr in list){                
                result.Append("<tr>").Append(aStr).Append("</tr>");
            }
            result.Append("</table>");
            return result.ToString();

        }

3.設(shè)定郵件發(fā)送時(shí)間,讀取郵件地址和內(nèi)容

  if (DateTime.Now.Minute == 10)
            {
                string mailBody = getMailBody(record);
                while (reader.Read())
                {
                    string aMail = reader["mail"].ToString();
                    SendEMail("'" + aMail + "'", "'" + aMail + "'", "考勤信息統(tǒng)計(jì)", "" + mailBody + "");
                }             
            }

            myconn.Close();    

DataTime.Now. xx  可以按照自己的需求去選擇發(fā)送郵件的間隔

3.對SendEmail()方法進(jìn)行編寫

  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;//發(fā)送郵件的優(yōu)先等級
  10. SmtpClient c = new SmtpClient("smtp.qq.com", 587);
  11. System.Net.NetworkCredential basicAuthenticationInfo =
  12. new System.Net.NetworkCredential("xxxxx@qq.com", "xxxxxx");//用戶名與SMTP授權(quán)碼
  13. c.Credentials = basicAuthenticationInfo;
  14. c.EnableSsl = true;//啟用SSL加密
  15. //c.Port = 587;
  16. c.Send(msg);
  17. }

4.新建一個(gè)web窗體,運(yùn)行程序


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多