0

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

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

之前感谢您的回答!

4

1 回答 1

3

您需要将电子邮件的内容类型定义为text/html,并且使用AlternateView,您可以将电子邮件以文本和 html 格式发送。您只需添加ContentTypeoftext/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>");
}
于 2013-08-03T22:49:06.060 回答