Format email when using system.web.mail

Posted on 2011-10-09
Last Modified: 2012-05-12
I have an aspx form that a user submits information, which is recorded to a database.

I want to send an email confirming their info. I am using a system.web.mail. Sending a static email works fine, but I want to include session variables of what they submitted and format the email such as put the data into a table.

I am having trouble formatting the text to send the HTML email.

Here is what I am trying...
I start with        

Session("EmailMsg") = "<b>Thank You for your Order: </b> " & Session("MyOrder") & "<br/>"

Then for each row of inforation the user submits I am doing this...

Session("EmailMsg") = Session("emailmsg") & lblItem1.Text & "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & lblDescp1.Text & "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & txtQty1.Text & "<br/>"

I have tried things like with no success

Session("EmailMsg") = Session("emailmsg") & "<table><td> lblItem2.Text</td> & "" & <td>lblDescp2.Text</td> & "" & <td>txtQty2.Text</td> &</table><br/> "

Can someone help me with formatting this email.

Thank You.
Question by:Wildone63
    LVL 11

    Accepted Solution

    I would start by using a StringBuilder. It will make it very clean to append additional text onto your email.
    Secondly, I would suggest using String.Format (or AppendFormat of the StringBuilder class). You can use numbered placeholders for the areas you want to inject without making all the concatenations. This reads easier as well...
    Dim sb As New System.Text.StringBuilder()
    sb.AppendFormat("<b>Thank You for your Order: </b> {0}<br/>", Session("MyOrder")
    sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", lblItem1.Text, lblDescp1.Text, txtQty1.Text)
    sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", lblItem2.Text, lblDescp2.Text, txtQty2.Text)
    Dim emailText = sb.ToString();

    Open in new window

    Your table was missing TR tags (for rows).
    LVL 11

    Expert Comment

    Excuse the last line, that's my C# showing through :)
    It should be:
    Dim emailText As String = sb.ToString()

    Open in new window

    LVL 1

    Author Closing Comment


    Thank You!

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 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

    16 Experts available now in Live!

    Get 1:1 Help Now