0

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("danieleluciani92@gmail.com");
    msg.To.Add("danieleluciani92@gmail.com");
    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("danieleluciani92@gmail.com", "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!

ProtoTyPus
  • 1,272
  • 3
  • 19
  • 40
  • If you're e-mail program (that you use to view the e-mail) understands HTML messages, then you won't see all the HTML markup - it will be used to render the content of the image. – Tim Aug 03 '13 at 22:46

1 Answers1

3

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("danieleluciani92@gmail.com");
    msg.To.Add("danieleluciani92@gmail.com");
    msg.Subject = txtSubject.Text;
    msg.IsBodyHtml = true;
    msg.Body = body;
    SmtpClient sc = new SmtpClient("smtp.gmail.com");
    sc.Port = 25;
    sc.Credentials = new NetworkCredential("danieleluciani92@gmail.com", "Dead2006!");
    sc.EnableSsl = true;
    sc.Send(msg);
    Response.Write("<script>alert('ennamo');</script>");
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • Thank you for your answer, I would ask you: CreateAlternateViewFromString(body, mimeType) <- body is? and then it says me: "ContentTyoe is 'property' but it's used as 'type' – ProtoTyPus Aug 03 '13 at 22:52
  • 1
    Explaining the difference between OP's code and your suggestions would make this a great answer, and help future readers who might be facing a similar issue. – Tim Aug 03 '13 at 22:52
  • @DanieleNekoLuciani, you shouldn't be getting that error message. Make sure your code is exactly like what I posted. I updated my answer with some explanation. – ataravati Aug 03 '13 at 23:01
  • Try hard-coding the html in your code, instead of getting it from txtBody.Text, and see if it works. – ataravati Aug 03 '13 at 23:05
  • @DanieleNekoLuciani, by the way, you are exposing your gmail's password in the code. I recommend you change your password. – ataravati Aug 03 '13 at 23:09
  • It's because asp.net does not allow you to post html code from a TextBox for security reasons. If you want to know how to change that, look here: http://stackoverflow.com/questions/4146309/in-a-text-box-in-asp-net-how-to-allow-it – ataravati Aug 03 '13 at 23:19
  • @ataravati I've tried with database and without security as they say in your link, but it doesn't work... still! TT^TT – ProtoTyPus Aug 03 '13 at 23:46
  • Try this: string body = HttpUtility.HtmlDecode(txtBody.Text); – ataravati Aug 04 '13 at 00:00