我收到了一项新要求,而这对我来说是新的。经过一两天的研究,我仍然无法弄清楚这一点。要求是找到一种方法将我们的“报告”页面作为电子邮件发送。这个报表页面有多个gridviews 和多个SqlDataSources 连接到它。这个页面在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);
}
}
这会发送电子邮件,但不幸的是,它只发送一个单词“返回”,即我的 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' 类型的控件 'FeaturedContent_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 = reader.ReadToEnd() 替换为 sendMail.Body += GetGridviewData(gvAlexandria) 并为每个网格视图添加了一个。现在所有网格视图都通过电子邮件发送。再次感谢所有的帮助!