如何发送电子邮件? [英] How to send an email?

查看:213
本文介绍了如何发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据表。

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:有一个<一个href="http://www.google.com/#hl=en&source=hp&q=smtpclient+quit&aq=f&aqi=g1&aql=&oq=&gs_rfai=&fp=8631cdd35a4d476d"相对=nofollow>错误,在 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.

这篇关于如何发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆