ASP.NET2.0邮件群发
首先添加命名空间
using System.Net.Mail;
using System.Data.SqlClient;
代码其实很简单,当然我这个功能比较简单
就是一个单击事件
protected void btn_send_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
//获取发送者地址
message.From = new MailAddress(tbx_address.Text.Trim());
//获取主题
message.Subject = tbx_subject.Text.Trim();
//获取发送邮件主题
message.Body = we_content.Text.Trim();
message.IsBodyHtml = true;
//设置数据库链接
SqlConnection conn = new SqlConnection(getstring());
conn.Open();
//数据查询用的是存储过程
SqlCommand cmd = new SqlCommand("SelectEmail", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter dutyparameter = cmd.Parameters.Add("@MemberDuty", SqlDbType.NVarChar, 20);
dutyparameter.Value = ddl_selectemail.SelectedValue;
SqlDataReader dr = cmd.ExecuteReader();
//执行查询获得要发往的邮件地址
while (dr.Read())
{
message.To.Add(dr["MemberEmail"].ToString());
}
conn.Close();
SmtpClient client = new SmtpClient();
client.Send(message);
lbl_info.Visible = true;
lbl_info.Text = "发送成功";
}
{
MailMessage message = new MailMessage();
//获取发送者地址
message.From = new MailAddress(tbx_address.Text.Trim());
//获取主题
message.Subject = tbx_subject.Text.Trim();
//获取发送邮件主题
message.Body = we_content.Text.Trim();
message.IsBodyHtml = true;
//设置数据库链接
SqlConnection conn = new SqlConnection(getstring());
conn.Open();
//数据查询用的是存储过程
SqlCommand cmd = new SqlCommand("SelectEmail", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter dutyparameter = cmd.Parameters.Add("@MemberDuty", SqlDbType.NVarChar, 20);
dutyparameter.Value = ddl_selectemail.SelectedValue;
SqlDataReader dr = cmd.ExecuteReader();
//执行查询获得要发往的邮件地址
while (dr.Read())
{
message.To.Add(dr["MemberEmail"].ToString());
}
conn.Close();
SmtpClient client = new SmtpClient();
client.Send(message);
lbl_info.Visible = true;
lbl_info.Text = "发送成功";
}
另外在web。config设置发送邮件的远程服务器连接
代码如下
<system.net>
<mailSettings>
<smtp>
<network host="smtp.163.com" port="25" userName="你的163帐号" password="你的密码" />
</smtp>
</mailSettings>
</system.net>
<mailSettings>
<smtp>
<network host="smtp.163.com" port="25" userName="你的163帐号" password="你的密码" />
</smtp>
</mailSettings>
</system.net>