2

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.

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
happysmile
  • 7,537
  • 36
  • 105
  • 181
  • @prince23: I think that it would be advisable to tag your question differently. First, it seems that you've already taken care of the `SQL` part. So this is mainly about sending `email`. (And the fact that you're doing this with `C#` raises the question whether you want to send mail using the `.NET` framework?) – stakx - no longer contributing Aug 16 '10 at 08:45
  • there may be many duplicate questions – Afshar Mohebi Aug 16 '10 at 10:26

4 Answers4

5

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3
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);
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
3
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));
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

You can send email from Data base .These articles might help you .

http://msdn.microsoft.com/en-us/library/ms190307.aspx
http://msdn.microsoft.com/en-us/library/ms189505.aspx

wizzardz
  • 5,664
  • 5
  • 44
  • 66