如何发送电子邮件? [英] How to send an email?
问题描述
我有这样的Excel表格。现在我正在读取数据并转换成如下的数据:
id名称MailID Body
123 kirna kiran@example.com生日快乐
234 ram ram@example.com生日快乐
345 anu anitha@example.com今天怎么样
357 rashmi rashmi @ example。 com工作需要完成
现在我向所有上述人发送电子邮件。
任何人都可以帮助我如何从datatable中读取数据,并向身体发送邮件给他们。
任何帮助都会很棒。
谢谢。
你可以使用 SmtpClient 类:
foreach(datatable.Rows中的DataRow行)
{
var name =(string)row [Name ];
var email =(string)row [MailID];
var body =(string)row [Body];
var message = new MailMessage();
message.To.Add(email);
message.Subject =这是主题;
message.From = new MailAddress(from@yourdomain.com);
message.Body = body;
var smtpClient = new SmtpClient(yoursmtphost);
smtpClient.Send(message);
}
Remark1:在.NET 4.0中, SmtpClient 实现 IDisposable ,所以请务必妥善处理。
备注2:有一个错误在 SmtpClient 类,不正确地向SMTP服务器发送 QUIT
命令。
I have an datatable like this.
I have an Excel sheet like this. Now I am reading the data from that and converting into an datatable like this:
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
Now I to send email to all the above person.
Can any one help me how I can read data from datatable and send mail to them with the body what is been specified.
Any help would be great.
Thanks.
You could use the SmtpClient class:
foreach (DataRow row in datatable.Rows)
{
var name = (string)row["Name"];
var email = (string)row["MailID"];
var body = (string)row["Body"];
var message = new MailMessage();
message.To.Add(email);
message.Subject = "This is the Subject";
message.From = new MailAddress("from@yourdomain.com");
message.Body = body;
var smtpClient = new SmtpClient("yoursmtphost");
smtpClient.Send(message);
}
Remark1: In .NET 4.0, SmtpClient implements IDisposable, so make sure to properly dispose it.
Remark2: There's a bug in SmtpClient class prior to .NET 4.0 which doesn't properly send the QUIT
command to the SMTP server.
这篇关于如何发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!