문제

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)
도움이 되었습니까?

해결책

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

다른 팁

To anyone also looking for something that will work went sent to email, you want to do as in @CResults answer, but add "%0A" to each line. This will get it through to most email clients.

You could also achieve this by sending the email with the body in HTML format like so:

When the body of the message is in HTML format, add the <br> tags right in your String. vbCrLf and StringBuilder don't work if the body is in HTML format.

Dim mail As New MailMessage
mail.IsBodyHtml = True
mail.Body = "First Line<br>"
mail.Body += "Second Line<br>"
mail.Body += "Third Line"

If it is not in HTML format, you use vbCrLf or vbNewLine:

Dim mail As New MailMessage
mail.Body = "First Line" & vbCrLf
mail.Body += "Second Line" & vbNewLine
mail.Body += "Third Line"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top