使用gridviews作为电子邮件发送aspx页面

时间:2013-09-27 13:15:33

标签: c# asp.net email gridview

我收到了一项新要求,这对我来说是新的。经过一两天的研究,我仍然无法弄明白这一点。要求是找到一种方式将我们的“报告”页面作为电子邮件发送。此报告页面具有多个网格视图和连接到它的多个SqlDataSource。这个页面对asp控件很重要。此页面属于使用表单身份验证的网站。我能够使这个页面可以查看未经身份验证的用户。我建议只发送链接到页面,即使他们没有登录,每个人都能看到该页面。这个想法似乎已经过度了,因为他们现在正在打印这个页面,将其作为pdf扫描,然后通过电子邮件发送给它。似乎它正在打败目的。我在aspx页面中放置了一个“发送电子邮件”按钮,然后单击一下我试图将此页面作为电子邮件的正文发送。这是我到目前为止所尝试的......

protected void btnEmail_Click(object sender, EventArgs e)
        {
            using (System.IO.StreamReader reader = System.IO.File.OpenText(Server.MapPath("~/Reporting/Report.aspx")))
            {
                string fromAddress = "fromaddress@something.com";
                string toAddress = "toaddress@something.com;
                System.Net.Mail.MailMessage sendMail = new System.Net.Mail.MailMessage(fromAddress, toAddress);
                sendMail.Subject = "Testing";
                sendMail.IsBodyHtml = true;
                sendMail.Body = reader.ReadToEnd();

                SmtpClient smtp = new SmtpClient("mail.something.com");
                smtp.Send(sendMail);

            }
        }

这会发送电子邮件,但不幸的是,它只发送一个单词“Back”,这是我的aspx页面中的链接按钮。这是一个单一的gridview示例,说明了我在电子邮件正文中发送的内容以及包含的按钮....

    <script lang="javascript" type="text/javascript">
        function printPage() {
            document.getElementById('<%= btnPrint.ClientID %>').style.display = 'none';
            document.getElementById('<%= lbBack.ClientID %>').style.display = 'none';
            document.getElementById('<%= btnEmail.ClientID%>').style.display = 'none';
            window.print();
            document.getElementById('<%= btnPrint.ClientID %>').style.display = 'none';
            document.getElementById('<%= lbBack.ClientID %>').style.display = 'none';
            document.getElementById('<%= btnEmail.ClientID%>').style.display = 'none';
        }
    </script>
    <div class="content-wrapper">
        <asp:LinkButton ID="lbBack" runat="server" OnClientClick="JavaScript:window.history.back(1);return false;">Back</asp:LinkButton>
        <asp:Button ID="btnPrint" runat="server" Text="Print" Font-Size="X-Small" Height="27px" Width="44px" OnClientClick="printPage()" />
        <asp:Button ID="btnEmail" runat="server" Font-Size="X-Small" Height="27px" OnClick="btnEmail_Click" Text="Send as Email" Width="105px" />
    </div>
    <div class="content-wrapper">
        <asp:Label ID="lblAlexandria" runat="server" Text="Alexandria" Font-Bold="True" Font-Size="Large"></asp:Label>
    </div>
    <div class="total-header" style="text-align: right">
        <asp:Label ID="lblTotalAlexandria" runat="server" Text="Total" BackColor="Black" ForeColor="White" Font-Bold="true"></asp:Label>
    </div>
    <asp:GridView ID="gvAlexandria" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="AlexandriaDataSource" GridLines="None" PageSize="200" HorizontalAlign="Center" ShowFooter="True" OnRowDataBound="gvAlexandria_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Dealership" HeaderText="Dealership" SortExpression="DEALER NAME" Visible="False">
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="DealDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="DealDate" SortExpression="DealDate">
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Location" HeaderText="Status" SortExpression="Location" Visible="False">
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField HeaderText="Bounced" SortExpression="Bounced" DataField="Bounced">
                <FooterStyle />
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" ForeColor="#CC0000" />
            </asp:BoundField>
            <asp:BoundField DataField="StockNumber" HeaderText="StockNumber" SortExpression="STOCK NO">
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Buyer" HeaderText="Buyer" SortExpression="LAST NAME">
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField HeaderText="Reason" SortExpression="Reason" DataField="Reason">
                <HeaderStyle Width="150px" BackColor="Black" ForeColor="White" HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="AmtFinanced" SortExpression="AmtFinanced">
                <ItemTemplate>
                    <asp:Label ID="lblAmtFinanced" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "AmtFinanced","{0:C}") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <%--<asp:Label ID="lblTotal" runat="server" Text="Total" BackColor="Black" ForeColor="White" Font-Bold="true"></asp:Label>--%>
                </FooterTemplate>
                <HeaderStyle BackColor="Black" ForeColor="White" />
                <ItemStyle HorizontalAlign="Right" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="D.O">
                <ItemTemplate>
                    <asp:Label ID="lblDaysOut" runat="server" Text='<%# Eval("DaysOut") %>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我不太确定我缺少什么,或者甚至可以将此页面作为电子邮件的正文发送。如果需要更多信息,请告诉我!谢谢!

编辑:在使用Sain Pradeep建议后,我收到了一个错误。错误是..

“GridView”类型的“Control'NatureContent_gvAlexandria'必须放在带有runat = server的表单标记内。”

为了解决这个问题,我插入了..

public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        } 

覆盖异常并正确发送电子邮件。我还从按钮单击中删除了“使用”,并使用sendMail.Body + = GetGridviewData(gvAlexandria)替换了sendMail.Body = reader.ReadToEnd(),并为每个gridview添加了一个。现在所有网格视图都会通过电子邮件发送再次感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

您将无法在电子邮件中发送任何“asp”代码并在另一端正确显示,您需要一个网络服务器才能将asp代码转换为屏幕上的可读格式。

您想要拥有一个单独的,不安全的页面。

另一个想法是将html'打印'到图像并发送。

另一种选择是生成一个页面,该页面输出与电子邮件客户端兼容的html,并将其作为正文通过电子邮件发送。

答案 1 :(得分:2)

请这样做

Msg.Body += GetGridviewData(gvUserInfo);

转换gridview数据的功能

 // This Method is used to render gridview control
public string GetGridviewData(GridView gv)
{
     StringBuilder strBuilder = new StringBuilder();
     StringWriter strWriter = new StringWriter(strBuilder);
     HtmlTextWriter htw = new HtmlTextWriter(strWriter);
     gv.RenderControl(htw);
     return strBuilder.ToString();
}

答案 2 :(得分:0)

为什么不使用wkhtmltopdf c# wrapperEvoPDF等工具将网页转换为PDF并将其作为附件发送电子邮件?

请注意: wkhtmltopdf c#库是免费的,而EvoPDF不幸是商业产品。

相关问题