ProtoTyPus
ProtoTyPus

Reputation: 1324

ASP.NET C# Insert Html code in Email

I can't understand a fact... I've created a form to send email and it goes really well. Now i've extended a textbox with an extended html of ajax control toolkit and, when I receive the email, I don't see all the code... i make an example. I've sent this:

<table><tbody><tr><td rowspan="2"> </td><td><b>LA QUALITA' ALPITOUR</b></td></tr><tr><td> </td></tr></tbody></table>

and in the email i see only the text bolded... all the other code is written and visible... why? I hope I've explained my problem well... I'm not english and I don't write it very well.

if it is needed, this is my code-behind:

protected void Btn_SendMail_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[email protected]");
    msg.To.Add("[email protected]");
    msg.Subject = txtSubject.Text;
    msg.IsBodyHtml = true;
    msg.Body = txtBody.Text;
    SmtpClient sc = new SmtpClient("smtp.gmail.com");
    sc.Port = 25;
    sc.Credentials = new NetworkCredential("[email protected]", "Dead2006!");
    sc.EnableSsl = true;
    sc.Send(msg);
    Response.Write("<script>alert('ennamo');</script>");
}

and this is my html/asp code:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<span class="fontS">A:
    <asp:TextBox ID="txtTo" runat="server" Font-Size="X-Small" ReadOnly="True">Tutti Gli Utenti</asp:TextBox>
    <br />
    Oggetto:
    <asp:TextBox ID="txtSubject" runat="server" Font-Size="X-Small" /><br />
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    </span>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <span class="fontS">Tipo di Email:</span><asp:DropDownList ID="DropDownList1" runat="server" Font-Size="X-Small" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Selected="True">Testo Libero</asp:ListItem>
            <asp:ListItem>Email Con Offerte</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" Visible="False" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
        </asp:CheckBoxList>
        <br />
        <asp:TextBox ID="txtBody" runat="server" Height="550px" TextMode="MultiLine" Width="900px" />
        <asp:HtmlEditorExtender ID="txtBody_HtmlEditorExtender" runat="server" TargetControlID="txtBody" EnableSanitization="False">
        </asp:HtmlEditorExtender>
    </ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="Btn_SendMail" runat="server" OnClick="Btn_SendMail_Click" Text="Invia" /><br />

Thanks before for your answers!

Upvotes: 0

Views: 3212

Answers (1)

ataravati
ataravati

Reputation: 9145

You need to define the content type of your email as text/html, and using AlternateView, you can send your email as both text and html. You just add ContentType of text/html as an alternate view, like this:

protected void Btn_SendMail_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();

    ContentType mimeType = new System.Net.Mime.ContentType("text/html");
    // Decode the html in the txtBody TextBox...    
    string body = HttpUtility.HtmlDecode(txtBody.Text);
    AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
    msg.AlternateViews.Add(alternate);

    msg.From = new MailAddress("[email protected]");
    msg.To.Add("[email protected]");
    msg.Subject = txtSubject.Text;
    msg.IsBodyHtml = true;
    msg.Body = body;
    SmtpClient sc = new SmtpClient("smtp.gmail.com");
    sc.Port = 25;
    sc.Credentials = new NetworkCredential("[email protected]", "Dead2006!");
    sc.EnableSsl = true;
    sc.Send(msg);
    Response.Write("<script>alert('ennamo');</script>");
}

Upvotes: 4

Related Questions