关于ASP.NET发送邮件问题。
数据库里有两个表,一个记录了邮件地址和部门名称A;另一个是记录部门详细信息的B,如下A:
MailAddress Department
a@company.com A
b@company.com B
.... ....
B:
Department EmpName Sex Age
A 1 男 1
A 1 男 1
B 2 男 2
B 2 男 2
... .... ... ...
现在的问题是。每次打开页面的时候怎么自动把相同B表中的记录发送到对应部门名称的邮件地址里。
--------------------编程问答--------------------
select B.* , A.MailAddress FROM B JOIN A ON B.Department = A.Department
--------------------编程问答--------------------
--------------------编程问答-------------------- 我还是贴出代码,请各位指点。谢谢了。
SqlConnection conn = new SqlConnection("连库字串");
string sql = "select A.MailAddress,B.* from A,B where A.Department=B.Department";
SqlDataAdapter adp = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
adp.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
//string to = dr["MailAddress"].ToString();
//body = .....;
}
}
try
{
MailMessage mailMsg = new MailMessage();
SqlCommand cmd = new SqlCommand("select * from A", Cn);
SqlDataReader dr = cmd.ExecuteReader();
////问题就在下面
while (dr.Read())
{
mailMsg.To.Add(dr["MailAddress"].ToString());//循环加入邮件地址。 //按不同部门先生成一个信息表格我是这样做的: switch (dr["department"].ToString())
{
case "A":
//用A作为参数传到select * from B where department='A',在SqlDataReader读取,最后用 StringBuilder生成一个A部门详细信息的表格 mailMsg.Body = "";//StringBuilder生成一个A部门详细信息的表格
break;
case "B":
.........
}
///问题出来了,我的方法结果是:所有地址都被发送了一个A部门的详细信息。
///我期望的是:a@Company.com收到的是A部门的详细信息,b@Company.com收到的是B部门的详细信息。 }
mailMsg.From = new MailAddress("my@company.com");
mailMsg.Subject = "自动发送的邮件";
mailMsg.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailMsg.IsBodyHtml = true;
SmtpClient mailServer = new SmtpClient();
mailServer.Host = "192.168.1.2";
mailServer.Send(mailMsg);
Response.Write("发送成功");
}
catch (Exception err)
{
Response.Write(err);
} --------------------编程问答-------------------- 哪位兄弟帮帮改一下。很急。在再次谢谢你们了。 --------------------编程问答-------------------- 收信人有2个 但你发送过程只有一次啊! 你的逻辑好像不对吧! 要把 mailServer.Send(mailMsg);
放到 while里面去吧!你可以一步一步调试啊 看看你的执行顺序 --------------------编程问答-------------------- 楼上兄弟。按你说的这段我改了一下确实是可以分开发送了。
但是发送到B的邮件又同时也发送一份给了A。
while (dr.Read())
{
mailMsg.To.Add(dr["MailAddress"].ToString());//循环加入邮件地址。
switch (dr["department"].ToString())
{
case "A":
mailMsg.Body = "";//StringBuilder生成一个A部门详细信息的表格
mailServer.Send(mailMsg);
break;
case "B":
.........
mailServer.Send(mailMsg);
}
改为if语句也是一样的。要如何才能发送给B的不发送送给A呢 --------------------编程问答--------------------
protected void btn_click(object sender, EventArgs e)
{
string sql = "";
string myconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection conn = new SqlConnection(myconn);
conn.Open();
string sqlstr = "select distinct Department from B";
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlDataReader read = comm.ExecuteReader();
while (read.Read())
{
switch (read["Department"].ToString())
{
case "A":
{
sql = "select MailAddress from A where Department='A'";
}
break;
case "B":
{
sql = "select MailAddress from A where Department='B'";
}
break;
}
sendmail(sql);
}
read.Close();
}
protected void sendmail(string sql)
{
string myconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection conn = new SqlConnection(myconn);
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader rd = com.ExecuteReader();
if (rd.Read())
{
string add = rd["MailAddress"].ToString();
MailAddress from = new MailAddress("my@company.com");
MailAddress to = new MailAddress(add);
MailMessage msg = new MailMessage(from, to);
msg.Subject = "自动发送的邮件";
msg.SubjectEncoding = Encoding.GetEncoding("GB2312");
msg.IsBodyHtml = true;
msg.Body = "";//StringBuilder生成一个A部门详细信息的表格
msg.BodyEncoding = Encoding.GetEncoding("GB2312");
SmtpClient Client = new SmtpClient("邮件服务器名");
Client.Send(msg);
Response.Write("发送成功");
}
rd.Close();
}
补充:.NET技术 , ASP.NET