Insert Dynamic table in HTML Email ASP.NET

Posted on 2012-08-25
Last Modified: 2012-08-27
I have the following email that I would like to include a bullet list or table with items that the recipient need to complete.   I won't know how many items per email until the SP is run.  How do I do that?  Thank you.

Private Sub RevisionEmail()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.GetOrderRevitionDetails_Email"
            cmd.Parameters.Add("@OrdersProductKey", Data.SqlDbType.Int).Value = Session("OrdersProductKey")
            Dim dt As New System.Data.DataTable

            Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader


            If dr.HasRows Then
                Const ToAddress As String = ""
                '(1) Create the MailMessage instance
                Dim mm As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
                '(2) Assign the MailMessage's properties
                mm.To.Add(New System.Net.Mail.MailAddress(ToAddress))
                mm.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("FromEmailAddress"))
                mm.Subject = "*** Please address QC Request(s) ASAP *** <%Address%>"
                mm.Body = "<table style='width:100%;'><tr><td>" +
                      "Hi <%Name%>,<br /><br />" +
                    " Please return to our web site, address the following QC requests and resubmit this report by XXXXX Pacific Time XX/XX/XX.  Thank you!  <br />" +
                   " Since this is an exterior only assignment, you will be free to schedule the inspection for a day and time that is most convenient for you (providing the due date and time will be honored).<br />" +
                    "</td> </tr>" +
                 "<tr style='background-color: #006699'><td style='color: #FFFFFF; font-weight: bold; font-size: medium valign='bottom'>Order Details</td></tr>" +
                " <tr>" +
                    " <td class='style4' >" +
                        " <table>" +
                        "     <tr>" +
                           "    <td>Order Id:&nbsp; </td><td><%OPK%></td>" +
                          "     </tr>" +
                            "   <tr>" +
                            "   <td>Property Address: </td><td><%Address%></td>" +
                               "  </tr>" +
                                "   <tr>" +
                            "   <td>Property City: </td><td><%City%></td>" +
                               "  </tr>" +
                                "   <tr>" +
                            "   <td>Property State: </td><td><%State%></td>" +
                               "  </tr>" +
                                "   <tr>" +
                            "   <td>Property Zip: </td><td><%Zip%></td>" +
                               "  </tr>" +
                               "  <tr>" +
                               "  <td>Due By:&nbsp;</td><td><%DueDay%> - <%DueDate%> <%DueTime%></td></tr>" +
                               "    <tr>" +
                                "   <td>Form Type: </td><td><%Report%></td>" +
                                "   </tr>" +
                               "    <tr>" +
                                 "  <td>Fee:&nbsp;</td><td><%Fee%></td></tr>" +
                                "   </table> </td></tr> " +
                              "  <tr style='background-color: #006699'>" +
                    "   <td style='color: #FFFFFF; font-weight: bold; font-size: medium' valign='bottom'>Special Instructions</td></tr>" +
                   "<tr><td class='style4'> <table><tr> <td><%Special%></td><td><label id='Label1'></label>" +
     "</td>  </tr> </table> </td></tr> <tr> <td class='style2'>" +
                " To accept this order <a href='<%OPK%>&OVK=<%OVK%>'  style='color:#0000FF; text-decoration:underline;' target='_blank'>Click here.</a><br />" +
                 "  To decline this order&nbsp;<a href='<%OPK%>&OVK=<%OVK%>' style='color:#0000FF; text-decoration:underline;' target='_blank'>Click here.</a>" +
                "   <br />" +
                 "  <br />" +
                "   <br />" +
                 "  NEED HELP?<br />" +
                 "  If you have any questions, please contact our team:<br />" +
                                 "  Thank you for your service!<br />" +
                 "  </tr>  </table>" +
                 "   <br />" +
                 "   <br />" +
                 "   <br />" +
                 "   <br />" +
                 "   <br />" +
                 " <p style='font-size: 9px; font-weight: normal; text-transform: uppercase; color: #000000'>FormID: MA-Ext</p>"
                mm.IsBodyHtml = True
                mm.Body = mm.Body.Replace("<%OPK%>", dr("OrdersProductKey"))
                mm.Body = mm.Body.Replace("<%Name%>", dr("VendorFirstName"))
                mm.Body = mm.Body.Replace("<%Address%>", dr("Address"))
                mm.Body = mm.Body.Replace("<%DueDay%>", dr("DueDay"))
                mm.Body = mm.Body.Replace("<%DueDate%>", dr("VendorDueDate"))
                mm.Body = mm.Body.Replace("<%Report%>", dr("ReportTypeName"))
                mm.Body = mm.Body.Replace("<%City%>", dr("City"))
                mm.Body = mm.Body.Replace("<%State%>", dr("StateAbbr"))
                mm.Body = mm.Body.Replace("<%Zip%>", dr("Zip"))

                '(3) Create the SmtpClient object
                Dim smtp As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

                '(4) Send the MailMessage (will use the Web.config settings)
            End If
        End Try
    End Sub

Open in new window

Question by:rcowen00
    1 Comment
    LVL 26

    Accepted Solution

    Hi rcowen00,

    You can use While to loop through the records/rows in the datareader.
    Create your open and close UL tags outside of the and populate the LI tags and content inside the loop.

    Dim strUnorderedList As String = "<ul>"
    While reader.Read
      strUnorderedList += "<li>" + Trim(reader("item_description").ToString()) + "</li>"
    End While
    strUnorderedList += "</ul>"

    Open in new window

    Alan ";0)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now