首页 > 数据库 >ASP.NET 定时发送邮件以及将数据库的数据以table形式发送

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

时间:2023-10-19 16:11:52浏览次数:32  
标签:ASP string reader2 发送 StudentSign ToString new msg table

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

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

 public void Send(object sender, System.Timers.ElapsedEventArgs e)
        {
            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();
 
            
            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();
            
 
            if (DateTime.Now.Minute == 10)
            {
                string mailBody = getMailBody(record);
                while (reader.Read())
                {
                    string aMail = reader["mail"].ToString();
                    //string msg = reader2["StuID"].ToString() +"  "+ reader2["StuName"].ToString() +"  "+ reader2["CourseName"].ToString() + "  " + reader2["SignState"].ToString() + "  " + reader2["OutState"].ToString();
                    SendEMail("'" + aMail + "'", "'" + aMail + "'", "考勤信息统计", "" + mailBody + "");
                }
                //endEMail sm = new SendMail();
                //SendEMail("'"+sql+"'", "'" + sql + "'", "Auto Mail", "This is a auto amil!");
            }
            myconn.Close();          
        }

3:筛选出需要接收邮件的邮箱地址--获取群发邮箱

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();
 

4;筛选出所需要的信息,创建链表,循环读取并储存在链表中,将字段数据以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();

5:生成读取邮件内容的方法,创建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();

        }

6:设定邮件发送时间,读取邮件地址和内容 这里也可设置  if (DateTime.Now.Day == 19)   //当前时间如果等19号  每个月19号都会自动发送

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

            myconn.Close();    

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

 public void SendEMail(string To1, string CC1, /*string BC1,*/ string Subject1, string Body1)
        {
            MailMessage msg = new MailMessage("[email protected]", To1);
            msg.CC.Add(CC1);
            //msg.Bcc.Add(BC1);
            msg.Subject = Subject1;
            msg.Body = Body1;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;//发送邮件的优先等级 
            SmtpClient c = new SmtpClient("smtp.qq.com", 587);
            System.Net.NetworkCredential basicAuthenticationInfo =
               new System.Net.NetworkCredential("[email protected]", "xxxxxx");//用户名与SMTP授权码
            c.Credentials = basicAuthenticationInfo;
            c.EnableSsl = true;//启用SSL加密 
            //c.Port = 587;
            c.Send(msg);
        }

 

8:程序运行会以邮件的方式发送出去

 

标签:ASP,string,reader2,发送,StudentSign,ToString,new,msg,table
From: https://www.cnblogs.com/xinyuzhu/p/17774933.html

相关文章

  • 在.net core 6.0 中 使用WebAPI进行QQ的邮件发送
    首先,是在工作中遇到的发邮件问题,但是自己还没有去实现,就先写了一个Demo。主要的内容是在网上搜的。下面进入正文。首先发邮件,第一步要确认发送的邮件的邮箱是那个邮箱的邮箱号,比较绕。就像我是拿QQ邮箱作为发件人,那么我就需要登录QQ邮箱,点开设置(由于我登录的是网站版的所以设置......
  • CompletableFuture异步优化代码
    CompletableFuture异步编排优化代码我们在项目开发中,有可能遇到一个接口需要调用N个服务的接口。比如用户请求获取订单信息,需要调用用户信息、商品信息、物流信息等接口,最后再汇总数据统一返回。如果使用串行的方法按照顺序挨个调用接口,这样接口的响应的速度就很慢。如果并行调用......
  • ASP.NET Core中DI中Add*方法对类的假定
    在ASP.NETCore的依赖注入(DI)容器中,当你使用Add*方法和泛型类型来指定要注册的类时,容器会做出以下假设:(1)类必须是具体类(ConcreteClass):使用Add*方法注册的类必须是一个具体的类,不能是接口或抽象类。这是因为你正在为特定服务类型注册一个实际的实现类。(2)类应该只有一个相关的构造......
  • vue +asp.net core webapi跨域
              vue代码:  login.vue  <template>  <divclass="login-wrap">      <el-buttontype="primary"style="width:100%;"@click="doSubmit()">提交</el-button>      ......
  • kubeadm init 报错ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables
    现象:[ERRORFileContent--proc-sys-net-bridge-bridge-nf-call-iptables]:/proc/sys/net/bridge/bridge-nf-call-iptablescontentsarenotsetto1原因:  /proc/sys/net/bridge/bridge-nf-call-iptables 文件的内容并没有设置为1解决方案echo"1">/proc/sys/net/br......
  • [912] Airtable automation related stuff
    ref:Airtableautomationtrigger:Whenrecordcreatedref:HowtoexportdatafromAirtableThe"Whenrecordcreated"triggeristriggered whenever anewrecordiscreated.Thismeansthatifyouaremanuallyenteringdata,thistriggerwillli......
  • Program does not contain a static 'Main' method suitable for an entry point
    http://www.kangry.net/blog/?article_id=391&type=article修改办法,对着项目右键-》属性-》application-》outputtype设为ClassLibrary即可。  ......
  • Spring Boot中发送邮件时,如何让发件人显示别名
    之前,我们通过一系列文章,介绍了如何在SpringBoot中发送邮件:发送邮件[1]添加附件[2]引用静态资源[3]邮件模版[4]已经包含了大部分的应用场景。但最近DD在做YouTube中文配音[5]的时候,碰到一个问题:如上图所示,收件人在客户端收到的时候,显示的名称是邮箱的前缀(no-reply),而不是我们的产......
  • FPGA, arduino, STM32, RaspBerry 树莓派 简介
    https://www.cnblogs.com/zhenghb31/p/15046496.html 市面上控制器这么多,似乎每一个都很厉害…为什么有的控制器编写起来那么难,功能很少,有的简单易学,功能强大呢?各种控制器又有什么区别呢?经过我的思考,我个人把控制器分为三类!第一类:基于逻辑电路的控制器(FPGA)FPGA(FieldPr......
  • element ui中table动态列切换时,表格样式变形或错乱
    现象:多个tab下切换显示不用的表格数据,且表头是动态渲染的,当操作栏浮动时, 表格显示的位置不对代码示例:<template> <el-table:data="data":key="toggleIndex":row-class-name="rowClassName":cell-class-name="cellClassName":header-cell-style......