[RESOLVED] [2008] Email through default email package - Newline not working
I am attempting to write some code that will launch the users default email application, and pre-fill the fields - To, Subject, Body
The Process.Start command launches my copy of Outlook and successfully puts everything in the correct fields.
The problem I am having is that the whole body appears on one line, regardless of either the 'vbCrLF' or 'Environment.Newline' or '<BR />' statements that appear.
For example
vb.net Code:
Dim sParams As String = ""
Dim subject = "This is a subject line"
Dim body = ""
body = Label1.Text & Environment.NewLine
body = body & Label3.Text & Environment.NewLine
body = body & Label2.Text
sParams = "mailto:" & emailto & "&subject=" & subject & "&body=" & body
System.Diagnostics.Process.Start(sParams)
For info - Label2.Text contains 2 short lines of text with a vbCrLf at the end of line 1.
Re: [2008] Email through default email package - Newline not working
To get it to work in a mailto, you need to properly encode the newline characters. You can do this by replacing the newline with %0d%0a or more simply, use the UrlEncode method, which will handle that for you. Also, the & before subject should be replaced with a ?. You may need to add a reference to System.Web to get the HttpUtility class.
Here is a qucik example:
Code:
Dim sParams As String = ""
Dim emailto = "[email protected]"
Dim subject = "This is a subject line"
Dim body = ""
body = "Hello" & Environment.NewLine
body = body & "World" & Environment.NewLine
body = body & "2"
body = System.Web.HttpUtility.UrlEncode(body)
sParams = "mailto:" & emailto & "?subject=" & subject & "&body=" & body
System.Diagnostics.Process.Start(sParams)
Re: [2008] Email through default email package - Newline not working
Thanks for your reply...
Your solution has fixed that problem but has created another minor problem! I added the reference to system.web, and added the line from your code above:-
vb.net Code:
body = System.Web.HttpUtility.UrlEncode(body)
and although the Newlines now work, every space has been changed to a '+' symbol. Now, I could do a body.replace - BUT I may have genuine '+' symbols in the body and it would not be good if I lost the ones that should be there since what I am actually creating is a routine to e-mail me some diagnostic information in case of the main application not working properly.
Re: [2008] Email through default email package - Newline not working
Use "%0A" for a new line.
Re: [2008] Email through default email package - Newline not working
vb.net Code:
Private Sub button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim str As String
str = "mailto:
[email protected]?body=The message's first paragraph.%0A%0aSecond paragraph.%0A%0AThird Paragraph."
Process.Start(str)
End Sub
Re: [2008] Email through default email package - Newline not working
Quote:
Originally Posted by Deepak Sakpal
vb.net Code:
Private Sub button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Dim str As String
str = "mailto:
[email protected]?body=The message's first paragraph.%0A%0aSecond paragraph.%0A%0AThird Paragraph."
Process.Start(str)
End Sub
FANTASTIC!! This works perfectly. Thank you! :thumb: