截取aspx页面body作为邮件的body发送

<%@ Page Language="C#" EnableEventValidation="false" ValidateRequest="false" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" language="javascript">
    function ClientGet()
    {

   //document.getElementById("Chart_prodCap").src = document.getElementById("Chart_prodCap").src;  //更改图片的src地址(相对→决定),比如,.net 3.5中的Chart控件生成的图片要通过Mail发送在邮件正文中,需加上这段,否则图片无法显示
        //result is a div.
        var result = document.getElementById("result");
        var hiddenInfo = document.getElementById("hiddenInfo");
        //alert(result.innerText);
        hiddenInfo.value = result.innerHTML;
    }   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="result">
        <input id="hiddenInfo" type="hidden" runat="server" name="hiddenInfo" />
        <asp:Button ID="btnSendMail" runat="server" Text="Button" OnClientClick="ClientGet();"
            onclick="btnSendMail_Click" />
            <p>这个是个测试</p>
    </div>
    </form>
</body>
</html>

-------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
   

    #region 發郵件
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        //this.ClientScript.RegisterStartupScript(this.GetType(), "", "ClientGet();", true);
        if (!string.IsNullOrEmpty(this.hiddenInfo.Value))
            this.SendMail(this.hiddenInfo.Value);
    }

    protected void SendMail(string body)
    {
        //邮件所通知的人
        string e_mail = "to@qq.com;";

        sendMailBySmtp(e_mail, "这是个神奇的邮件", body);


    }
    private void sendMailBySmtp(string T, string S, string B)
    {
        MailMessage myMail = new MailMessage();
        myMail.From = "from@qq.com";
        myMail.To = T.Trim();
        myMail.Subject = S.Trim();
        //myMail.Priority = MailPriority.High;  //邮件级别,.High、.Low、.Normal
        myMail.BodyFormat = MailFormat.Html;  //邮件形式,.Text、.Html
        myMail.Body = B.Trim();

        myMail.BodyEncoding = System.Text.Encoding.UTF8;

        myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//1表示服务器需要用户名密码验证
        myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username");
        myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
        SmtpMail.SmtpServer = "192.168.1.1";
        SmtpMail.Send(myMail);  //发送邮件

    }
    #endregion

}

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. –Martin Fowler
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/gerryge/p/2226282.html