using System;
using System.Collections.Generic;using System.Linq;using System.Net.Mail;using System.Web;using System.Data;using System.Data.SqlClient;using System.Configuration;/// <summary>/// EMailTo 的摘要说明/// </summary>public class EMailTo{ public string SendMail(DataTable dt_to,string subjects,string bodys) { DataTable dtemail = QueryWorkerAndEmail(); System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); foreach (DataRow row in dt_to.Rows) { foreach (DataRow item in dtemail.Rows) { if (row["WorkNumber"].ToString().Trim().ToUpper() == item["WorkNumber"].ToString().Trim().ToUpper()) { msg.To.Add(item["Email"].ToString()); break; } } } //msg.To.Add("zhangping_it@lhys.cc"); //msg.To.Add(b@b.com); /* * msg.To.Add("b@b.com");可以发送给多人 * msg.CC.Add("c@c.com");可以抄送给多人 */ msg.From = new MailAddress("服务器邮箱", "在线考试系统", System.Text.Encoding.UTF8); /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ msg.Subject = subjects;//邮件标题 msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 msg.Body = bodys;//邮件内容 msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 msg.IsBodyHtml = true;//是否是HTML邮件 msg.Priority = MailPriority.Normal;//邮件优先级SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("服务器邮箱", "密码"); //在71info.com注册的邮箱和密码 client.Host = ""; //smtp. object userState = msg; try { client.SendAsync(msg, userState); //简单一点儿可以client.Send(msg); return "发送成功!"; } catch (System.Net.Mail.SmtpException ex) { return "发送失败," + ex; } } //初始化员工信息 public DataTable QueryWorkerAndEmail() { SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LogInConnectionString"].ConnectionString); Conn.Open(); string strSQL = "SELECT TOP 1000 [WorkNumber] ,[Email] FROM [OnLineExam].[dbo].[EmployeEmail]"; SqlDataAdapter da = new SqlDataAdapter(strSQL.ToString(), Conn); //实例化 DataSet 数据集 这个数据集会绑定在gridview上 DataSet data = new DataSet(); //向数据集中fill(填入)数据 da填入data da.Fill(data, "table"); return data.Tables[0]; }}