如何发送电子邮件?

发布于 2024-09-14 11:14:36 字数 944 浏览 4 评论 0原文

我有一个这样的数据表。

我有一张这样的Excel表格。现在我正在读取其中的数据并转换为如下数据表:

id   Name     MailID                Body

123  kirna    [email protected]     happy birthday   
234  ram      [email protected]       happy birthday  
345  anu      [email protected]    how is the day going
357  rashmi   [email protected]    work need  to be completed

现在我要向上述所有人员发送电子邮件。

任何人都可以帮助我如何从数据表中读取数据并向他们发送邮件,其中包含指定的正文。

任何帮助都会很棒。

谢谢。

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    [email protected]     happy birthday   
234  ram      [email protected]       happy birthday  
345  anu      [email protected]    how is the day going
357  rashmi   [email protected]    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-09-21 11:14:36

类:

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("[email protected]");
    message.Body = body;
    var smtpClient = new SmtpClient("yoursmtphost");
    smtpClient.Send(message);
}

您可以使用 SmtpClient :在.NET 4.0中, SmtpClient 实现IDisposable,因此请确保正确处置它。

备注2:有一个SmtpClient 类无法正确将 QUIT 命令发送到 SMTP 服务器。

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("[email protected]");
    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.

忆离笙 2024-09-21 11:14:36
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 <[email protected]>", "Recipients Name <[email protected]>", "subject", "body");
// }
client.Send(message);
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 <[email protected]>", "Recipients Name <[email protected]>", "subject", "body");
// }
client.Send(message);
书间行客 2024-09-21 11:14:36
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));
    }
}
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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文