欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-06-13 08:45:08|栏目:.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#程序设计有所帮助。

上一篇:ASP.NET生成树形显示的GridView实现思路

栏    目:.NET代码

下一篇:Asp.Net Core Identity 隐私数据保护的实现

本文标题:C#实现按数据库邮件列表发送邮件的方法

本文地址:http://www.codeinn.net/misctech/141095.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有