欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现按数据库邮件列表发送邮件的方法

程序员文章站 2023-11-20 22:31:52
本文实例讲述了c#实现按数据库邮件列表发送邮件的方法。分享给大家供大家参考。具体实现方法如下: using system; using system.net;...

本文实例讲述了c#实现按数据库邮件列表发送邮件的方法。分享给大家供大家参考。具体实现方法如下:

using system;
using system.net;
using system.net.mail;
using system.text;
using system.threading;
delegate void senddelegate(string from, string to, string subject, string body, string host, int port, string username, string password);
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="from">发件人</param>
/// <param name="to">收件人</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="host">发送服务地址(smtp.qq.com)</param>
/// <param name="port">发送邮件服务器端口(25) int型</param>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
public void sendmail(string from, string to, string subject, string body, string host, int port, string username, string password)
{
 mailmessage message = new mailmessage(from, to, subject, body);
 message.isbodyhtml = true;
 message.bodyencoding = text.encoding.utf8;
 message.attachments.add(new attachment("c:\\log.log"));
 smtpclient client = new smtpclient(host, port);
 client.credentials = new networkcredential(username, password);
 client.deliverymethod = smtpdeliverymethod.network;
 client.send(message);
}
sqlconnection conn = new sqlconnection();
conn.connectionstring = "data source=(local);integrated security=sspi;initial catalog=db_showhouse";   //打开连接
conn.open();
sqlcommandcmd = new sqlcommand("select email from employee", conn);
sqldatareader drnew = cmd.executereader();
if (drnew.hasrows)
{
 while (drnew.read())
  new senddelegate(sendmail).begininvoke("someone@somecompany.com",drnew[0].tostring(),"subject","body","smtp.somescompany.com",25,"username","password");
}
drnew.close();

希望本文所述对大家的c#程序设计有所帮助。