Method of Sending Mail by Database Mail List Based on C
- 2021-07-18 08:47:25
- OfStack
This article illustrates the method of C # to send mail by database mailing list. Share it for your reference. The specific implementation method is as follows:
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>
/// Send e-mail
/// </summary>
/// <param name="from"> Sender </param>
/// <param name="to"> Recipient </param>
/// <param name="subject"> Mail Subject </param>
/// <param name="body"> Mail content </param>
/// <param name="host"> Send service address (smtp.qq.com)</param>
/// <param name="port"> Outgoing mail server port (25) int Type </param>
/// <param name="userName"> User name </param>
/// <param name="password"> 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"; // Open a connection
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();
I hope this article is helpful to everyone's C # programming.