Search code examples
vb.netnewlinetostringstringbuildercarriage-return

Visual Basic - Sending a Carriage Return to an E-Mail


How can I send a string to an email with new lines (carriage returns) included?

The problem is that I am sending the string to an email, and the email strips out the carriage returns.

Dim myApp As New Process

    emailStringBuilder.Append("mailto:")

    emailStringBuilder.Append("&subject=" & tmpID & " - " & subject)

    emailStringBuilder.Append("&body= " & msgStringBuilder.ToString)

    myApp = System.Diagnostics.Process.Start(emailStringBuilder.ToString)

Solution

  • I'm guessing you're adding to your StringBuilder without newlines as I've just tested it and it works fine for me.

    Imports System.Text
    Module Module1
        Sub Main()
            Dim sb As New StringBuilder
            sb.AppendLine("Line 1")
            sb.AppendLine("Line 2")
            sb.Append("Line 3" & vbCrLf)
            sb.Append("Line 4 without CRLF")
            sb.Append("Line 5")
            Console.WriteLine(sb.ToString)
        End Sub
    End Module
    

    For the above code I get the following output

    Line 1
    Line 2
    Line 3
    Line 4 without CRLFLine 5
    

    Hope this helps.

    EDIT

    Ok, so based on the new information (about the email) the above still holds true. If you add to a stringbuilder with .Append you will lose, or more correctly, not see newlines. Instead you must use .AppendLine which will add the all important CR and LF codes onto the end of your string.

    However, I am a little confused how you are sending email. I've seen it done this way from a webpage before but never from vb.net. Sending an email this way will almost certainly force you to send an email without newlines!!

    Can I suggest you look at the the following from Microsoft on how to send email from Visual Basic using the System.Web.Mail namespace. It's not hard and you'll get a lot more control over the email you send this way....

    Microsoft example