Avatar of jwebster77
jwebster77

asked on 

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

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)

Open in new window

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.
Visual Basic.NET

Avatar of undefined
Last Comment
jwebster77
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jwebster77
jwebster77

ASKER

Thanks a lot! This worked:
Great, your advice worked!
sb.AppendLine("Number of customers who received an email: " & r("CountCol").ToString & "<br><br>")
 errMessage.Body = sb.ToString()
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo