c#.net 群发邮件
2011年11月14日,新公司第一天的工作。
新建邮件类Mails如下:
public class Mails
{
public Mails(string Psmtp, string Pfrom, string Ppwd, string Pto, string Psubject, string Pbody)
{
this.smtp = Psmtp;//要通过什么邮箱服务器发送邮件。例:smtp="smtp.qq.com";
this.from = Pfrom;//发件人的邮箱地址。例:from = "761529715@qq.com";
this.pwd = Ppwd;//发件人的邮箱密码。例:pwd = "*********";
this.to = Pto;//收件人的邮箱地址。例:to = "1780100810@qq.com";
//this.to.Replace(" ","");
this.subject = Psubject;//邮件的标题。例:subject = "nihao";
this.body = Pbody;//邮件的内容。例:body = "woshiwuhan,nishishuia?";
}
//...
//字段与属性的没粘过来
//...
public string SendMail()
{
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = smtp;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from,pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage(from,to);
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
//Response.Write("<script type='text/javascript'>alert('发送成功')</script>");
return "发送成功!";
}
catch (Exception ex)
{
//Response.Write("<script type='text/javascript'>alert('"+ex.Message+"')</script>");
return ex.Message;
}
}
}
**************************************************************************
protected void btn_SendMail_Click(object sender, EventArgs e)
{
List<string> lists = new List<string>();
lists=EmailSubscribeServices.selEmail();//获取收件人邮箱地址
Mails mySendMail=new Mails();
for (int i = 0; i < lists.Count; i++)
{
mySendMail = new Mails(lists[i].ToString());
txt_SendMail.Text += lists[i].ToString()+ mySendMail.SendMail()+"\n";
}
}
**************************************************************************
namespace DAL
{
public static class EmailSubscribeServices
{
public static List<string> selEmail()
{
string sql = "select email from 你的数据表";
DataTable dt=new DataTable ();
List<string> lists = new List<string>();
//string result;
try
{
dt=DBHelper.GetDataSet(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
lists.Add(dt.Rows[i][0].ToString());
}
return lists;
}
catch(Exception ex)
{
lists.Add(ex.Message);
return lists;
}
}
}
}
***************************************************************************
namespace DLL
{
/// <summary>
/// 数据访问抽象基础类
/// </summary>
public static class DBHelper
{
//数据库连接字符串
public static string sqlconnectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//...只列出了用到的DBHelper
//取得所有,不带参数
internal static DataTable GetDataSet(string safeSql)
{
SqlConnection connection = new SqlConnection(sqlconnectionstring);
connection.Open();
DataSet ds = new DataSet();
try
{
SqlCommand cmd = new SqlCommand(safeSql, connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch
{
}
finally
{
connection.Close();
}
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return new DataTable();
}
}
}
}
项目里运行没有问题,这是从项目中考出的代码,没有测试。
优质内容筛选与推荐>>