C# 如何发送电子邮件?
我有一个这样的数据表 我有一张这样的Excel表格。现在我从中读取数据,并将其转换为如下数据表:C# 如何发送电子邮件?,c#,.net,sql,email,ado.net,C#,.net,Sql,Email,Ado.net,我有一个这样的数据表 我有一张这样的Excel表格。现在我从中读取数据,并将其转换为如下数据表: id Name MailID Body 123 kirna kiran@example.com happy birthday 234 ram ram@example.com happy birthday 345 anu anitha@example.com how is the day g
id Name MailID Body
123 kirna kiran@example.com happy birthday
234 ram ram@example.com happy birthday
345 anu anitha@example.com how is the day going
357 rashmi rashmi@example.com work need to be completed
现在我想给上述所有人发电子邮件
有谁能帮我从datatable中读取数据,并用指定的正文向他们发送邮件
任何帮助都会很好
谢谢。System.Net.Mail.SmtpClient=
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("yoursmtp.server.com");
// foreach row in datatable{
System.Net.Mail.MailMessage message =
new System.Net.Mail.MailMessage("Your Name <from@domain.com>", "Recipients Name <to@domain.com>", "subject", "body");
// }
client.Send(message);
新系统.Net.Mail.SmtpClient(“yourstp.server.com”);
//数据表中的foreach行{
System.Net.Mail.MailMessage消息=
新系统.Net.Mail.MailMessage(“您的姓名”、“收件人姓名”、“主题”、“正文”);
// }
客户端。发送(消息);
您可以使用该类:
注意:在.NET4.0中,实现了,所以请确保正确地处理它
注意:在.NET 4.0之前的类中有一个未正确将QUIT
命令发送到SMTP服务器的类。private IEnumerable GetMessages()
private IEnumerable<Tuple<string,string,string>> GetMessages()
{
using (var connection = new SqlConnection("connection string")
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT Name, MailID, Body FROM table";
connection.Open()
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return new Tuple<string,string,string>(
reader.GetString(0), // name
reader.GetString(1) // email
reader.GetString(2)); // body
}
}
}
}
foreach(var tuple in GetMessages())
{
SendMessage(tuple.Item1, tuple.Item2, tuple.Item3);
}
private void SendMessage(string name, string email, string body)
{
using (var smtpClient = new SmtpClient("smtp.example.com"))
{
smtpClient.Send(new MailMessage(
name, // from
email, // to
"Subject",
body));
}
}
{
使用(var connection=newsqlconnection(“连接字符串”)
使用(var command=connection.CreateCommand())
{
command.CommandText=“从表中选择名称、邮件ID、正文”;
connection.Open()
使用(var reader=command.ExecuteReader())
{
while(reader.Read())
{
产生返回新元组(
reader.GetString(0),//名称
reader.GetString(1)//电子邮件
reader.GetString(2));//正文
}
}
}
}
foreach(GetMessages()中的变量元组)
{
SendMessage(tuple.Item1、tuple.Item2、tuple.Item3);
}
私有void发送消息(字符串名称、字符串电子邮件、字符串正文)
{
使用(var smtpClient=new smtpClient(“smtp.example.com”))
{
smtpClient.Send(新邮件)(
姓名,//from
电子邮件,//发送至
“主题”,
机构),;
}
}
您可以从数据库发送电子邮件。这些文章可能会对您有所帮助
@prince23:我认为最好给你的问题加上不同的标签。首先,你似乎已经处理好了
SQL
部分。所以这主要是关于发送电子邮件。(事实上,您使用C#
进行此操作时,会提出一个问题:您是否希望使用.NET
框架发送邮件?)可能会有许多重复的问题
private IEnumerable<Tuple<string,string,string>> GetMessages()
{
using (var connection = new SqlConnection("connection string")
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT Name, MailID, Body FROM table";
connection.Open()
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return new Tuple<string,string,string>(
reader.GetString(0), // name
reader.GetString(1) // email
reader.GetString(2)); // body
}
}
}
}
foreach(var tuple in GetMessages())
{
SendMessage(tuple.Item1, tuple.Item2, tuple.Item3);
}
private void SendMessage(string name, string email, string body)
{
using (var smtpClient = new SmtpClient("smtp.example.com"))
{
smtpClient.Send(new MailMessage(
name, // from
email, // to
"Subject",
body));
}
}