私は新しい要件を手渡されましたが、これは私にとって新しいものです。1日か2日の調査の後、私はまだこれを理解することができません. 要件は、「レポート」ページを電子メールとして送信する方法を見つけることです。このレポート ページには、複数のグリッドビューと複数の SqlDataSources が接続されています。このページは、asp コントロールに非常に重きを置いています。このページは、フォーム認証を使用する Web サイトに属しています。認証されていないユーザーがこのページを閲覧できるようにすることができました。ページへのリンクを送信するだけで、ログインしていなくても誰もがページを表示できるようになることをお勧めします。彼らは現在、このページを印刷し、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ページにあるリンクボタンです。これは、含まれているボタンと一緒に電子メールの本文で送信しようとしているものの単一のグリッドビューの例です....
<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. */
}
これは例外をオーバーライドし、電子メールを正しく送信します。また、ボタンクリックから「Using」を削除し、sendMail.Body = reader.ReadToEnd() を sendMail.Body += GetGridviewData(gvAlexandria) に置き換え、グリッドビューごとに 1 つ追加しました。すべてのグリッドビューが電子メールで送信されるようになりました。すべての助けをありがとう!