none
如何将gridview做为邮件的内容发送出去 RRS feed

  • 问题

  • 大家好,
    我的页面有一个grdiview,我想知道如何把我的gridview,做为邮件的内容发送出去。
    发送到指定人的邮箱。
    谢谢
    2016年10月20日 1:43

全部回复

  • 把你的GridView导出到Excel样式,然后作为附件发出去即可。

    至于如何导出为GridView,请参考:

    http://blog.csdn.net/loveheronly/article/details/6711474


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    2016年10月20日 1:47

  • 参看这篇文章,GridView导出Excel

    http://www.codeproject.com/Tips/477436/Export-Gridview-Data-to-Excel-in-ASP-NET

    发送邮件参考这里

    http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms



    2016年10月20日 1:53
  •  你好,

    据我所知,我们可以使用GridView1.RenderControl的方法来把Gridview以html的形式发送出去。

    具体你可以参照如下代码:

    Gridview:

    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
        RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
        runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="80" />
            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
            <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="btnSendEmail" runat="server" Text="Send email" OnClick="SendEmail" />

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    
    protected void SendEmail(object sender, EventArgs e)
    {
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                GridView1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
                mm.Subject = "GridView Email";
                mm.Body = "GridView:<hr />" + sw.ToString(); ;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "sender@gmail.com";
                NetworkCred.Password = "<password>";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }
    }
     
    public override void VerifyRenderingInServerForm(Control control)
    {
        //用于重写否则会报错
    }
    	


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已建议为答案 Angie Xu 2016年11月10日 5:24
    2016年10月26日 5:45