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("[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!

1
  • 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. Commented Aug 3, 2013 at 22:46

1 Answer 1

4

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>");
}
Sign up to request clarification or add additional context in comments.

8 Comments

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'
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.
@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.
Try hard-coding the html in your code, instead of getting it from txtBody.Text, and see if it works.
@DanieleNekoLuciani, by the way, you are exposing your gmail's password in the code. I recommend you change your password.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.