将动态表格或div内容作为电子邮件正文内容发送

Lon*_*der 5 c# asp.net sendmail .net-4.0

我有一个页面(somePage.aspx),我需要作为电子邮件正文生成的内容

<div id="DV_TimeReportWraper" runat="server" style="display:block">
    <table id="TBL_UsersinTblTime">
       <tr id="TR_UsersinTblTime">
         <td id="TD_H_Name" class="Dg">
             name                
         </td>
         <td id="TD_H_UserID" class="Dg">
             ID
         </td>
         <td id="TD_H_Status" class="Dg">
             initial Stage
         </td>
         <td id="TD_H_SignOutAutoExeState" class="Dg">
             current Stage
         </td>
       </tr>
       <%
           if(edata != null)
               for (int idx=0;idx<edata.Count;idx++) {
                   var row = edata[idx];
                   bool bgcl = (idx % 2) == 0;
                   string BgCol = "";
                   if (bgcl)
                       BgCol = "#70878F";
                   else
                       BgCol = "#E6E6B8";
       %>
       <tr style=" background-color:<%=BgCol%>">
           <td id="TD_Name">
               <% = row["name"] %>
            </td>
            <td id="TD_UserID">
                <%= row["UserId"]%>
            </td>
            <td id="TD_Status">
                <%
                    int uidForSrc = Convert.ToInt32(row["UserId"]);
                    string src = "images/SignedOutNoFrame.png";
                    if (UserDidnotSignOutTimeOut(uidForSrc))
                        src = "images/didnotSignOut.png";
                 %>
                 <input type="image" src="<% =src %>" style="width:25px" />
             </td>
             <td id="TD_SignOutAutoExeState" >
                 <% 
                     string EexcSrc = "";
                     string inputType ="hidden";
                     //this updates if needed then returns true= needed update false = isn't need
                     if (UpdatedTimeOutOKForUSER(uidForSrc))
                     {
                         inputType = "image";
                         excSrc = "/images/SignedOutNoFrame.png";
                     }
                 %>
                 <input type="<%=inputType %>" src="<%=EexcSrc %>" style="width:25px" />
             </td>
        </tr>
        <%
            if (idx == edata.Count - 1)
                sendLog();
            }
        %>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

代码 sendLog()

public void sendLog()
{
    mail.aReciver="username@gmail.com";
    mail.bSubject="ttest";
    mail.cBody = DV_UsersInTblTime.InnerHtml;
    mail.HentalSend();
}
Run Code Online (Sandbox Code Playgroud)

我无法获得要分配的内容的价值mail.cBody.
它说的是关于价值不是文字等的东西.

这是我在外部类中使用的方法,在最后一次尝试将页面内容的功能添加为正文之前工作正常,如何在此处根据需要实现结果?

public static class mail
{
    public static string aReciver, bSubject, cBody;
    public static void HentalSend()
    {
        string SmtpServer = "smtp.gmail.com";
        int port = 587;
        string sender = "Sender@domain.com";
        string ReCiver = aReciver;
        string Subject = bSubject;
        string Body = cBody;
        string account = "mail@domain.com";
        string Pass = "123456";
        Send(SmtpServer, port, account, Pass, sender, Receiver, Subject, Body);
        ... //send() is another relevant method to this question, it does rest of mail settings
    }
}
Run Code Online (Sandbox Code Playgroud)

bor*_*per 8

此代码将生成的动态控件的HTML转换为字符串变量.

StringBuilder stringBuilder = new StringBuilder();
StringWriter writer = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
try {
    DV_TimeReportWraper.RenderControl(htmlWriter);
} catch (HttpException generatedExceptionName) {
}

string DV_TimeReportWraper_innerHTML = stringBuilder.ToString();
Run Code Online (Sandbox Code Playgroud)

然后只使用DV_TimeReportWraper_innerHTML您的电子邮件正文

如果此控件具有子控件,则可能必须创建循环.更多相关内容:http://msdn.microsoft.com/en-us/library/htwek607.aspx#Y472