导出数据以及邮件发送

正则表达式使用:

正则表达式.test(要验证的数据)  返回true 或者 false

正则表达式去百度搜就行。

后台以表格的形式导出数据:

 1  //1、要导出哪些数据,需要一个泛型集合
 2         List<car> list = con.car.ToList();
 3 
 4         //2、拼接成table表格的格式
 5         StringBuilder str = new StringBuilder();//这个类相等于+=,但是比较节省控件
 6 
 7         str.Append("<table border='1'>");
 8         foreach (car c in list)
 9         {
10             str.Append("<tr>");
11             str.Append("<td>" + c.ids + "</td>");
12             str.Append("<td>" + c.code + "</td>");
13             str.Append("<td>" + c.name + "</td>");
14             str.Append("<td>" + c.brand + "</td>");
15             str.Append("<td>" + c.time + "</td>");
16             str.Append("<td>" + c.oil + "</td>");
17             str.Append("<td>" + c.powers + "</td>");
18             str.Append("<td>" + c.exhaust + "</td>");
19             str.Append("<td>" + c.price + "</td>");
20             str.Append("<td>" + c.pic + "</td>");
21             str.Append("</tr>");
22         }
23         str.Append("</table>");
24         //3、导出到服务器指定路径
25         string path = Server.MapPath("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");
26         StreamWriter wr = new StreamWriter(path);
27         wr.Write(str);
28         wr.Close();//*********************************流用完了要关闭
29 
30         //4、给用户下载
31         Response.Redirect("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");

用一般处理程序操作输出数据:

 1 //引用命名空间
 2 using System.Linq;
 3 using System.Data.Linq;
 4 using System.Collections;
 5 using System.Collections.Generic;
 6 using System.Text;
 7 
 8 
 9 
10 
11 
12  DataClassesDataContext con = new DataClassesDataContext();
13     public void ProcessRequest (HttpContext context) {
14         context.Response.ContentType = "appliction/vnd.ms-excel";//使用输出流的固定格式
15         //context.Response.ContentType = "appliction/msword";
16         List<car> list = con.car.ToList();
17 
18         StringBuilder str = new StringBuilder();
19         str.Append("<table border='1'>");
20         foreach (car c in list)
21         {
22             str.Append("<tr>");
23             str.Append("<td>" + c.ids + "</td>");
24             str.Append("<td>" + c.code + "</td>");
25             str.Append("<td>" + c.name + "</td>");
26             str.Append("<td>" + c.brand + "</td>");
27             str.Append("<td>" + c.time + "</td>");
28             str.Append("<td>" + c.oil + "</td>");
29             str.Append("<td>" + c.powers + "</td>");
30             str.Append("<td>" + c.exhaust + "</td>");
31             str.Append("<td>" + c.price + "</td>");
32             str.Append("<td>" + c.pic + "</td>");
33             str.Append("</tr>");
34         }
35         str.Append("</table>");
36 
37         context.Response.Write(str);//输出
38         context.Response.End();//结束输出
39     }
40  

在后台用Response.Redirect();打开一般处理程序使用,但是很多浏览器会把一般处理程序下载下来,不好用。

发邮件:

 1 //创建SMTP调用服务类
 2         SmtpClient smtp = new SmtpClient("smtp.sina.cn");//这个字符串去邮箱里面找,是发邮件要调用的服务器,服务状态要改成开启
 3 
 4         //创建发送人对象
 5         MailAddress aaa = new MailAddress("18560812711@sina.cn");
 6         //创建接收人对象
 7         MailAddress to = new MailAddress(TextBox1.Text);
 8         //创建邮件对象
 9         MailMessage mail = new MailMessage(aaa,to);
10         //填充邮件主题
11         mail.Subject="来自于起航科技用户注册的验证码邮件";
12         //填充邮件内容
13         mail.Body="您的验证码为[1234],请在20分钟内填写,此邮件为系统邮件,勿回复!";
14         Session["YZM"] = "1234";
15         //注册证书,验证发送人邮箱和密码
16         NetworkCredential net = new NetworkCredential("18560812711@sina.cn", "hq1234561");
17         //将证书关联到服务器对象等待验证
18         smtp.Credentials = net;
19 
20         //调用发送方法
21         smtp.Send(mail);
原文地址:https://www.cnblogs.com/mazhijie/p/5794553.html