简体   繁体   中英

How can I send “<asp:datalist>” content with c# to an email?

So I have this problem : I have my Datalist on asp.net, but i don't know how to retrive the data for sending it to an email. I have tried appending it on a string but I think it's not the solution. Can someone help me please?

<asp:DataList ID="listaSuper" runat="server" OnItemDataBound="listaSuper_ItemDataBound"                  
              Width="100%" RepeatColumns="1" RepeatDirection="Horizontal" 
              DataKeyNames="IdListaSuperDetalle"
              OnItemCommand="DataList_projects_ItemCommand">
 <ItemTemplate>
    <table>
        <tr>
            <td>
                <asp:Image ID="Image3" Style="width: 200px; height: 200px" runat="server" ImageUrl='<%# Eval("UrlImg", "{0}") %>' />
            </td>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </td>
            <td>
                <div class="sCursos" style="padding: 5px 0 0 5px;">
                    <table>
                        <tr>
                            <td style="width: 40px; padding: 4px;">
                                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "IdListaSuperDetalle") %>'
                                    ImageUrl="~/images/LstSuper/tache.png" />
                            </td>
                            <td>
                                <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Receta")%>'></asp:Label>
                                <asp:Label ID="lblreceta" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "IdReceta")%>'
                                    Visible="false"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="sCursos" style="padding: 5px 0 0 5px;">
                    <br />
                    <table>
                        <tr>
                            <td>
                                <asp:DataList ID="listaSuperdd" runat="server" DataKeyField="Idreceta">
                                    <ItemTemplate>
                                        <table style="width: 100%; margin-left: 10%">
                                            <tr>
                                                <td style="width: 40px; padding: 4px;">
                                                    <asp:CheckBox ID="chkIngredientes" runat="server" Checked="true" />
                                                </td>
                                                <td align="left">
                                                    <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Descripcion")%>'></asp:Label>
                                                </td>
                                                <td style="width: 20px; padding: 4px;">
                                                    &nbsp;
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:DataList>
                                <br />
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
</ItemTemplate>

And my C# code is this:

Note that I'm not sending anything from the datalist because I don't know how I can make it... And in this moment I'm frustrated... so here it is:

protected void EnviarCorreo(object sender, ImageClickEventArgs e)
{
    int id = Convert.ToInt32(Request.QueryString["Id"]);

    EntCheft Chef = new EntCheft();

    if (Session["User"] != null)
    {
        Chef = Session["User"] as EntCheft;

        LyncottClient objCliente = new LyncottClient();
        WsLyncott.EntCheft Resultado = new WsLyncott.EntCheft();
        Resultado = Session["User"] as WsLyncott.EntCheft;

        string sNombre = string.Format("{0} {1}", Chef.Nombre, Chef.ApellidoP);
        MailAddress ToMail = new MailAddress(Resultado.Email, sNombre);
        cCorreo CorreoRegistro = new cCorreo();

        var txtMsgReceta = new StringBuilder();
        divmensaje.RenderControl(new HtmlTextWriter(new StringWriter(txtMsgReceta)));

        string s = txtMsgReceta.ToString();
        txtMsgReceta.Append("<html><head><title></title></head><body style='background-color:White'>")
            .Append("<table border='0'> <tr> <td style='width: 70%;'><img src='../images/impReceta/hoja-impresion_01.jpg' width='199' height='112'/> </td>")
             .Append("<td align='right'>")
             .Append("MI LISTA DE SUPER")
             .Append("<br /> </td></tr>")
             .Append(" <tr> </tr></table>")
             .Append("<table><tr><td>  <img src='../images/impReceta/hoja-impresion_24.jpg' width='645' height='28' /> </td> </tr></table></body>");



        CorreoRegistro.EnviarCorreoLocal(ToMail, sNombre, Resultado.Email,txtMsgReceta, "Receta..");

    }
    Response.Redirect("LstSuper.aspx");
}

   public string SendMailPruebalocal(string Subject, MailAddress ToMailAddress, MailAddress FromMailAddress, string Body)
{


    MailMessage msg = new MailMessage();
    msg.From = FromMailAddress;

    msg.To.Add(ToMailAddress); 
    //msg.Bcc.Add("contacto@*****.com");
    msg.Subject = Subject;//Asunto
    msg.Body = Body;  //Cuerpo del correo
    msg.IsBodyHtml = true;
    msg.Priority = MailPriority.Normal;

    SmtpClient SmtpServer = new SmtpClient();

    SmtpServer.Credentials = new System.Net.NetworkCredential("******", "*****");
    SmtpServer.Port = 587;
    SmtpServer.Host = "smtp.gmail.com";
    SmtpServer.EnableSsl = true;
    try
    {
        SmtpServer.Send(msg);
        msg.Dispose();
        return "Se envio la información correctamente.";

    }
    catch (Exception ex)
    {
        throw ex;
    }


}



public void EnviarCorreoLocal(MailAddress ToMail, string sNombre, string User, StringBuilder txtMsgReceta, string titulo)
{
    MailAddress FromMail = new MailAddress("contacto@*****.com", "*********");
    SendMailPruebalocal(titulo, ToMail, FromMail, txtMsgReceta.ToString());

}

You need to convert the data inside the DataList into HTML, you can't simply just add it. Luckily, this is easy enough using HtmlTextWriter and the web control's RenderControl method.

var htmlTxtWriter = new HtmlTextWriter(new System.IO.StringWriter(txtMsgReceta , System.Globalization.CultureInfo.InvariantCulture));

listaSuperdd.RenderControl(htmlTxtWriter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM