Search code examples
vb.netemailline-breaks

Email line breaks not working


I am trying to force line breaks inside the body of my automatic email.

I have referenced many examples, such as "Adding multiple lines to body of SMTP email VB.NET".

I've tried many different ways to make line breaks:

  1. StringBuilder.AppendLine

    Dim sb As New StringBuilder
    sb.AppendLine("For security purposes, only the password will be provided within this email.")
    sb.AppendLine("Thank you and have a wonderful day.")
    
  2. System.Environment.NewLine

    strbody = strbody & "Thank you and have a wonderful day. " & System.Environment.NewLine
    strbody = strbody & "Password: " & Dsa.Tables(0).Rows(0).Item(2) & System.Environment.NewLine & System.Environment.NewLine
    
  3. vbNewLine

    strbody = strbody & "Thank you and have a wonderful day. " &  vbNewLine
    
  4. vbCrLf

    strbody = strbody & "Thank you and have a wonderful day. " & vbCrLf
    
  5. Environment.NewLine

    strbody = strbody & "Thank you and have a wonderful day. " & Environment.NewLine
    

and so on, but every time I receive the email there are no line breaks.

Is there any other versions I can test?

Dim strbody As String

Try

    Dim strTo As String

     strbody = ""
    strbody = strbody & "Thank you for contacting us to gain account access. " & vbNewLine
    strbody = strbody & "You requested your password for the Applicaton registration site. " & vbNewLine
    strbody = strbody & "For security purposes, only the password will be provided within this email. " & vbNewLine
    strbody = strbody & "Thank you and have a wonderful day. " & vbNewLine
    strbody = strbody & "Password: " & Dsa.Tables(0).Rows(0).Item(2) & System.Environment.NewLine & vbNewLine & vbNewLine
    strbody = strbody & "If you didn't request your password, please notify from@location.com of your concern "


    strTo = Dsa.Tables(0).Rows(0).Item(1)
    Dim msg As New System.Net.Mail.MailMessage("from@we-location.com", strTo, "Retrive Your Password", strbody)

    Dim smtp As New System.Net.Mail.SmtpClient


    msg.IsBodyHtml = True
    smtp.Host = "mailhost.location.com"
    smtp.Send(msg)

Catch ex As Exception

End Try

Solution

  • The key line of code here is this:

    msg.IsBodyHtml = True
    

    Since you are sending an HTML email you must use HTML markup to create line breaks.

    strbody = strbody & "Thank you for contacting us to gain account access. <br><br>"