|
-
Jul 17th, 2008, 05:12 PM
#1
Thread Starter
Addicted Member
[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.
Last edited by christopherpm; Jul 17th, 2008 at 05:18 PM.
If my post helped you, please rate it. Thanks.
-
Jul 17th, 2008, 06:18 PM
#2
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)
-
Jul 18th, 2008, 02:03 AM
#3
Thread Starter
Addicted Member
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.
Last edited by christopherpm; Jul 18th, 2008 at 02:08 AM.
If my post helped you, please rate it. Thanks.
-
Jul 18th, 2008, 02:35 AM
#4
Re: [2008] Email through default email package - Newline not working
Use "%0A" for a new line.
-
Jul 18th, 2008, 02:37 AM
#5
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
-
Jul 18th, 2008, 02:50 AM
#6
Thread Starter
Addicted Member
Re: [2008] Email through default email package - Newline not working
 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!
If my post helped you, please rate it. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|