ASP.NET C#在电子邮件中插入Html代码

时间:2013-08-03 22:41:10

标签: c# html asp.net email

我无法理解事实...... 我已经创建了一个发送电子邮件的表单,它非常好。 现在我扩展了一个带有扩展的ajax控件工具包html的文本框,当我收到电子邮件时,我没有看到所有代码......我做了一个例子。 我发了这个:

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

在电子邮件中,我只看到粗体文本...所有其他代码都是可写的......为什么? 我希望我已经很好地解释了我的问题......我不是英语,而且写得不好。

如果需要,这是我的代码隐藏:

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>");
}

这是我的html / asp代码:

<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 />

先谢谢你的回答!

1 个答案:

答案 0 :(得分:3)

您需要将电子邮件的内容类型定义为text/html,并使用AlternateView,您可以将您的电子邮件同时发送为文本和HTML。您只需添加ContentType text/html作为备用视图,如下所示:

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>");
}