C#實現(xiàn)按數(shù)據(jù)庫郵件列表發(fā)送郵件的方法
更新時間:2015年07月15日 16:59:12 作者:DTC2
這篇文章主要介紹了C#實現(xiàn)按數(shù)據(jù)庫郵件列表發(fā)送郵件的方法,涉及C#讀取數(shù)據(jù)庫及通過自定義函數(shù)發(fā)送郵件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)按數(shù)據(jù)庫郵件列表發(fā)送郵件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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> /// 發(fā)送電子郵件 /// </summary> /// <param name="from">發(fā)件人</param> /// <param name="to">收件人</param> /// <param name="subject">郵件主題</param> /// <param name="body">郵件內(nèi)容</param> /// <param name="host">發(fā)送服務地址(smtp.qq.com)</param> /// <param name="port">發(fā)送郵件服務器端口(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#程序設(shè)計有所幫助。
相關(guān)文章
Unity的IPreprocessBuild實用案例深入解析
這篇文章主要為大家介紹了Unity的IPreprocessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Winform實現(xiàn)抓取web頁面內(nèi)容的方法
這篇文章主要介紹了Winform實現(xiàn)抓取web頁面內(nèi)容的方法,代碼只有短短幾行,但是功能很實用,需要的朋友可以參考下2014-09-09