Create HTML table with c # from a datatable

1

Good afternoon everyone.

What I need is to create an HTML table from c #, the data I need I have in a datatable, this in order to send an email from a class and in the body must go a table.

Searching the web I could see that it can be achieved by creating a table in this way but I do not know how to start, as a comment I already have created my class to send the email.

    string cuerpo = "";
    public DataTable Detalles;
    MailMessage mail = new MailMessage();

    public void EnviarCorreo(string correo_electronico)
    {            
        int NumeroFilas = Detalles.Rows.Count;
        cuerpo = "Se prestaron " + NumeroFilas + " dispositivos a su 
        nombre";
        SmtpClient SmtpServer = new SmtpClient();
        SmtpServer.Credentials = new 
        System.Net.NetworkCredential("Direccion", "password");
        SmtpServer.Port = 587;
        SmtpServer.Host = "smtp.gmail.com";
        SmtpServer.EnableSsl = true;

        mail = new MailMessage();
        try
        {
            mail.From = new MailAddress("Direccion de envío", "Prestamo", 
            System.Text.Encoding.UTF8);
            mail.Subject = "Prestamo";
            //Justo acá es donde necesito insertar mi tabla
            mail.Body = cuerpo;
            mail.To.Add(correo_electronico);
            SmtpServer.Send(mail);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Thank you in advance.

    
asked by JDiego9708 03.06.2017 в 19:35
source

1 answer

1

A string must go in the body. So you must go through the data and go concatenating. I mean, something like that.

String tabla="<table>
foreach (Dato in datos)
   tabla+="<tr>"+dato+"</tr>";

and then

mail.Body =tabla
    
answered by 05.06.2017 / 13:08
source