none
Richtextbox text to outlook doesn't carry line feeds RRS feed

  • Question

  • Hi Gurus,

    I've copied & modified my VBA code to send an email via MS Outlook. In the Access DB the Outlook email body from the email form would appear as formatted in the textbox on the form but I can't get that to happen in VB.Net

    The textbox is multiline & when I type in it & press enter key it does a line feed as expected but that doesn't carry through to the email body where it's just a long line of text.

    Here are the relevant parts of code from the form: The calling parameters are Subject, emailbody, display email, ishtmlformat, if embedded, to address, cc address, bccaddress, attachments, signature, email account

            Dim strSignat As String = My.Settings.EmailSignature, strDefAcc As String = My.Settings.DefaultEmailAcc, txtMsg As String = System.Net.WebUtility.HtmlEncode(txtBody.Text)
    
                    If strSendTo.Length > 0 Then
                        If Tag = 1 Then
                            EmailAsHtml(txtSubject.Text, txtMsg, True, False, If(String.IsNullOrEmpty(txtEmbedded.Text), False, True),,, strSendTo.ToString,, txtAttach.Text, strSignat, strDefAcc)
    

    And in a module

    Function EmailAsHtml(strSubj As String, strBody As String, blnDisplayEmail As Boolean, IsHtmlFormat As Boolean, Embedded As Boolean, Optional strAddr As String = "", Optional strAddrCC As String = "", Optional strAddrBCC As String = "", Optional strRecpt As String = "", Optional strAttach As String = "", Optional strSignature As String = "", Optional strEmailAccount As String = "")
            Dim strSendAcc As String = ""
    
                With oMail
                    .Subject = strSubj
                    If IsHtmlFormat And Embedded = True Then
                        .HTMLBody = strBody & "<br><br>" & .HTMLBody & "<br>" & Signature
                    ElseIf IsHtmlFormat = False And Embedded = True Then
                        .HTMLBody = WebUtility.HtmlEncode(strBody) & "<br><br>" & WebUtility.HtmlEncode(.HTMLBody) & "<br>" & Signature
                    ElseIf IsHtmlFormat Then
                        .HTMLBody = strBody & "<br>" & Signature
                    Else
                        .HTMLBody = WebUtility.HtmlEncode(strBody) & "<br>" & Signature        'This will convert the plain text string to an Html string
                    End If
    
                        .SendUsingAccount = oNameSpace.Session.Accounts.Item(strSendAcc)
    
    I've tried everything I can think of & am still stumped. Anyone got an idea what I'm missing or why the behaviour should be different? Any pointers & help greatly appreciated

    Monday, September 24, 2018 4:49 PM

Answers

  •  Not sure if this may or may not be useful but,  if you are actually getting the text from a RichTextBox,  then be aware that a RichTextBox only uses a single LineFeed character to indicate the end of each line unlike a TextBox which normally uses a CarriageReturn character followed by a LineFeed character to indicate the end of a line.

     Maybe,  your lines/strings from the RichTextBox need to have the CarriageReturn character inserted into them so they will contain the standard CarriageReturn and LineFeed characters.  For example,  the below code would get the RichTextBox.Text and replace all LineFeed characters with a CarriageReturn and LineFeed character.  Then just use the RtbTextWithCrLfs string variable instead of the RichTextBox.Text.

    Dim RtbTextWithCrLfs As String = RichTextBox1.Text.Replace(Chr(10), Chr(13) & Chr(10))

     Maybe it will help?  8)


    If you say it can`t be done then i`ll try it

    Monday, September 24, 2018 5:57 PM

All replies

  • Just a point - Do you have txtMsg content in proper HTML format? 

    Sincerely, Highly skilled coding monkey.

    Monday, September 24, 2018 5:07 PM
  • Before including the text to the body, try replacing the line separator with “<br>”, for example: ‘.HTMLBody = strBody.Replace(vbCrLf, "<br>") & "<br>" & Signature’, etc.


    Monday, September 24, 2018 5:21 PM
  •  Not sure if this may or may not be useful but,  if you are actually getting the text from a RichTextBox,  then be aware that a RichTextBox only uses a single LineFeed character to indicate the end of each line unlike a TextBox which normally uses a CarriageReturn character followed by a LineFeed character to indicate the end of a line.

     Maybe,  your lines/strings from the RichTextBox need to have the CarriageReturn character inserted into them so they will contain the standard CarriageReturn and LineFeed characters.  For example,  the below code would get the RichTextBox.Text and replace all LineFeed characters with a CarriageReturn and LineFeed character.  Then just use the RtbTextWithCrLfs string variable instead of the RichTextBox.Text.

    Dim RtbTextWithCrLfs As String = RichTextBox1.Text.Replace(Chr(10), Chr(13) & Chr(10))

     Maybe it will help?  8)


    If you say it can`t be done then i`ll try it

    Monday, September 24, 2018 5:57 PM
  • Hi Andrey

    The txtmsg is the textbox.text per this statement txtMsg As String = System.Net.WebUtility.HtmlEncode(txtBody.Text)

    I've tried it as ishtmlformat false & true with no success

    Monday, September 24, 2018 6:03 PM
  • If tried the following changes & sending as ishtmlformat true & as false without any success

                    .Subject = strSubj
                    If IsHtmlFormat And Embedded = True Then
                        .HTMLBody = strBody & "<br><br>" & .HTMLBody & "<br>" & Signature
                    ElseIf IsHtmlFormat = False And Embedded = True Then
                        .HTMLBody = WebUtility.HtmlEncode(strBody) & "<br><br>" & WebUtility.HtmlEncode(.HTMLBody) & "<br>" & Signature
                    ElseIf IsHtmlFormat Then
                        .HTMLBody = strBody.Replace(vbCrLf, "<br>") & "<br>" & Signature
                    Else
                        .HTMLBody = WebUtility.HtmlEncode(strBody.Replace(vbCrLf, "<br>")) & "<br>" & Signature        'This will convert the plain text string to an Html string
                    End If
    

    Monday, September 24, 2018 6:08 PM
  • Hello,

    without any success

    Yes, yes...

    Do you, by all what you did, distinct two moments:

    - is a problem with how oMail.HTMLBody handled proper HTML document

    - is a problem with what you are assigned to oMail.HTMLBody

    If you didn't find where is a problem - how do you planing to solve a problem?

    Get a proper HTML document and see if it is properly handled on sending. 

    If it is send correctly - look on what you assign to the property.

    If not - look how to get it right and only after try to send something else.


    Sincerely, Highly skilled coding monkey.


    Monday, September 24, 2018 6:38 PM
  • Try

    oMail.IsBodyHtml=True



    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, September 24, 2018 6:41 PM
  • > Maybe it will help?

    I didn't think so... rtf-format are very far from HTML. Some kind of converter require...


    Sincerely, Highly skilled coding monkey.

    Monday, September 24, 2018 6:43 PM
  • Try

    oMail.IsBodyHtml=True



    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Just kidding, noticed youre using OL.

    Why not use .Net?


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, September 24, 2018 6:48 PM
  • Hi IronRazerz,That worked thank you

    Monday, September 24, 2018 7:57 PM
  • Hi Gtripodi,

    I'm always keen to learn more & since I'm teaching myself VB.net maybe you could give me a heads up on how to use .net for this instead?

    Monday, September 24, 2018 8:00 PM