-2

I want to insert database content in Email body, index 0 of data reader which contain id number for each email address, I want to send mail using add id number in mail body for instance: The Employee id is 5. I need insert each id on each mail body which exist in same row. I have used this code to insert id in body but that didn't work

message.Body = "The Employee id is 5  "+reader[1].ToString();

Full code is:

MailMessage message = new MailMessage();
message.Subject = "Employee Access ";

message.From = new MailAddress("avvv@gmail.com");
var fromAddress = "avvv@gmail.com";
const string fromPassword="password";
var smtp = new System.Net.Mail.SmtpClient();
{
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
    smtp.Timeout = 20000;


    OleDbCommand cmd = null;
    OleDbCommand cmd2 = null;

    string queryString = "select id,email,status from tableemail";
    using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = xe;
    Password=654321;User ID = xpress; unicode=true"))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        cmd = new OleDbCommand(queryString);
        cmd.Connection = connection;
        OleDbDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            MailAddress to = new MailAddress(reader[1].ToString());
            message.To.Add(to);   
        }

        message.Body = "The Employee id is 5  "+reader[1].ToString();
        smtp.Send(message);
        reader.Close();
    }
}

Can I insert multiple database field in body using data reader?

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
moonir01
  • 88
  • 1
  • 10

2 Answers2

0

I am not sure exactly how your code needed this functionality however from my understanding you can do it something like this. using DataTable

    DataTable dataTable = new DataTable();
    MailMessage message = new MailMessage();
    message.Subject = "Employee Access ";

    message.From = new MailAddress("avvv@gmail.com");
    var fromAddress = "avvv@gmail.com";
    const string fromPassword = "password";


    OleDbCommand cmd = null;

    string queryString = "select id,email,status from tableemail";
    using (OleDbConnection connection = new OleDbConnection("Provider = OraOLEDB.Oracle.1; Data Source = xe; Password = 654321; User ID = xpress; unicode = true"))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        cmd = new OleDbCommand(queryString);
        cmd.Connection = connection;
        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
        adapter.Fill(dataTable);
        adapter.Dispose();
    }

    foreach (DataRow dataRow in dataTable.Rows)
    {
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;

            MailAddress to = new MailAddress(dataRow[1].ToString());

            if (!message.To.Contains(to))
            {
                    message.To.Add(to);
            }

            message.Body = "The Employee id is 5  " + dataRow[0].ToString();
            smtp.Send(message);
        }
    }
Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46
0

I think the issue is you are calling the DataReader after it has read the last record. Reader.Read() returned a false, meaning reader[1] should throw an error since there is nothing to read.

I don't know if you wanted every value in the body of the email or just the last one... assuming you wanted every one, something like this should work:

using (OleDbConnection connection = new OleDbConnection(cs))
{
    connection.Open();

    using (OleDbCommand cmd = new OleDbCommand(queryString, connection))
    {
        using (OleDbDataReader reader = cmd.ExecuteReader())
        {
            StringBuilder body = new StringBuilder();

            while (reader.Read())
            {
                string to = reader.GetString(0);
                message.To.Add(to);
                body.AppendFormat("The Employee id is 5 {0}<br>", to);
            }

            message.Body = body.ToString();
            reader.Close();
        }
    }
}
smtp.Send(message);

If you really only want the last one (as your original code implies), I would just save it in a local string within the loop and then add it after the reader loop.

On an unrelated matter, you are instantiating your OLE Command object twice. I eliminated the duplicate in the code snippet above.

Hambone
  • 15,600
  • 8
  • 46
  • 69