Results 1 to 6 of 6

Thread: [RESOLVED] [2008] Email through default email package - Newline not working

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Resolved [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:
    1. Dim sParams As String = ""
    2.  
    3.         Dim emailto = "[email protected]"
    4.         Dim subject = "This is a subject line"
    5.         Dim body = ""
    6.  
    7.         body = Label1.Text & Environment.NewLine
    8.         body = body & Label3.Text & Environment.NewLine
    9.         body = body & Label2.Text
    10.  
    11.  
    12.         sParams = "mailto:" & emailto & "&subject=" & subject & "&body=" & body
    13.         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.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    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:
    1. 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.

  4. #4

  5. #5
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] Email through default email package - Newline not working

    vb.net Code:
    1. Private Sub button1_Click( _
    2.     ByVal sender As Object, _
    3.     ByVal e As EventArgs _
    4. ) Handles Button1.Click
    5.  
    6.     Dim str As String
    7.     str = "mailto:[email protected]?body=The message's first paragraph.%0A%0aSecond paragraph.%0A%0AThird Paragraph."
    8.     Process.Start(str)
    9. End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Re: [2008] Email through default email package - Newline not working

    Quote Originally Posted by Deepak Sakpal
    vb.net Code:
    1. Private Sub button1_Click( _
    2.     ByVal sender As Object, _
    3.     ByVal e As EventArgs _
    4. ) Handles Button1.Click
    5.  
    6.     Dim str As String
    7.     str = "mailto:[email protected]?body=The message's first paragraph.%0A%0aSecond paragraph.%0A%0AThird Paragraph."
    8.     Process.Start(str)
    9. 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
  •  



Click Here to Expand Forum to Full Width