C#完成按數據庫郵件列表發送郵件的辦法。本站提示廣大學習愛好者:(C#完成按數據庫郵件列表發送郵件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成按數據庫郵件列表發送郵件的辦法正文
本文實例講述了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#法式設計有所贊助。