troubleshooting Question

VB.Net MailMessage is not creating a line break in the email

Avatar of jwebster77
jwebster77 asked on
Visual Basic.NET
3 Comments1 Solution20 ViewsLast Modified:
Hi, I have the following code which it should create 4 lines, therefore it should insert 3 break lines but it is not doing that.  It is just creating one single line in the body of the email.  I have used stringBuilder. I can't understand why.  Could you please help?  Thank you.

        Dim errMessage As MailMessage
        Dim body As String
        Dim sb As New System.Text.StringBuilder
        Dim dataTbl As New Data.DataTable
        ............

            Dim r As DataRow
            Dim count As Integer = 0
            For Each r In dataTbl.Rows
                count += 1
                If count = 1 Then
                    sb.AppendLine("Number of customers who received an email: " & r("CountCol").ToString)
                    'body += "Number of customers who received an email: " & r("CountCol").ToString & vbCrLf & "<br>"
                    body = sb.ToString() & vbCrLf
                End If
                If count = 2 Then
                    sb.AppendLine("Number of email recipients: " & r("CountCol").ToString)
                    'body += " Number of email recipients: " & r("CountCol").ToString & vbCrLf & "<br>"
                    body = body & "Test" & vbCrLf
                End If
                If count = 3 Then
                    sb.AppendLine("Number of customers with invoices but no email address: " & r("CountCol").ToString)
                    'body += " Number of customers with invoices but no email address: " & r("CountCol").ToString & vbCrLf & "<br>"
                End If
                If count = 4 Then
                    sb.AppendLine("Number of customers with an invalid email address: " & r("CountCol").ToString)
                    'body += " Number of customers with an invalid email address: " & r("CountCol").ToString & vbCrLf & "<br>"
                End If
            Next


        Dim errClient As SmtpClient
        Dim errFrom As MailAddress
        Dim errTo As MailAddress

        errFrom = New MailAddress("n@field.com")
        errTo = New MailAddress("ns@aero.com")
        errMessage = New MailMessage(errFrom, errTo)
        errMessage.Subject = "Errors in AR Statement"
        errMessage.IsBodyHtml = True
        
        errMessage.Body = sb.ToString()
        'errMessage.Body = body

        errClient = New SmtpClient(SMTPServer)
        errClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials
        errClient.Send(errMessage)
Alternatively I have also tried to save the string in a string variable called "body"(you can see it commented out) and use vbCrLf  or  <br>, but it still does not work.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros