本文實(shí)例講述了C#實(shí)現(xiàn)按數(shù)據(jù)庫郵件列表發(fā)送郵件的方法。分享給大家供大家參考。具體實(shí)現(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ā)送服務(wù)地址(smtp.qq.com)</param>/// <param name="port">發(fā)送郵件服務(wù)器端口(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();希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選