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();
}
}


}

}

项目里运行没有问题,这是从项目中考出的代码,没有测试。

优质内容筛选与推荐>>
1、Column store index 列数据如何匹配成行数据?
2、hello
3、BackbOne移动端框架学习(2):backbone中的Model
4、bzoj 3384: [Usaco2004 Nov]Apple Catching 接苹果
5、PHP大小写是否敏感问题的汇总PHP代码


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号