如何发送电子邮件?发送电子邮件

2023-09-03 01:45:10 作者:Onlyone挚爱

我有一个这样的数据表。

i have an datatable like this.

我有一个Excel工作表是这样的。现在我从读取数据并将其转换成一个像这样的数据表

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.

任何帮助将是巨大的。

感谢

推荐答案

您可以使用的 SmtpClient 类:

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);
}

备注1:在.NET 4.0中, SmtpClient 工具 IDisposable的的,所以一定要正确处理它。

Remark1: In .NET 4.0, SmtpClient implements IDisposable, so make sure to properly dispose it.

备注2:有一个错误,在 SmtpClient 类之前,.NET 4.0不正确地发送退出命令到SMTP服务器。

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.