keeping line feeds in textbox to email message

manared

Well-known member
Joined
Jun 1, 2006
Messages
84
Programming Experience
1-3
I'm using vb.net 2005 and I have a multiline textbox where users enter an address. This textbox has up to 6 lines of text. The user can click a button to email of the information on the form to someone. Right now I am just using the textbox name.text in an html formatted email. How would I go about keeping those enters/line feeds in the email?

Here are some code snippets:

VB.NET:
eMsg += "<td width=300 valign='top'>To: " & txtAddress.Text

VB.NET:
 Dim Emessage As String = eMsg.ToString.Trim 
            Try 
                Dim objMail As New System.Net.Mail.MailMessage(Efrom, Eto, Esubject, Emessage) 

                Dim filename As String = "" 
                Dim y As Integer = 0 
                For y = 0 To dset.Tables(0).Rows.Count - 1 
                    If dset.Tables(0).Rows(y).Item(6).ToString = "" Then 
                        'do nothing, move on 
                    Else 
                        filename = dset.Tables(0).Rows(y).Item(6).ToString.Trim 
                        objMail.Attachments.Add(New System.Net.Mail.Attachment(filename)) 
                    End If 
                Next 

                objMail.IsBodyHtml = True 
                objMail.Priority = Net.Mail.MailPriority.Normal 
                Dim objSMTP As New System.Net.Mail.SmtpClient("serverName") 
                objSMTP.Send(objMail) 
            Catch ex As Exception 
                MsgBox(ex.ToString) 
            End Try
 
Use the System.String.Replace method to replace NewLine characters with the corresponding html Newline tag. Example:
VB.NET:
eMsg &= "<td width=300 valign='top'>To: " & _
  txtAddress.Text.Replace(Environment.NewLine, "<br />").Trim
Notice the use of the & operator instead of the + operator. From VS help:
The & Operator (Visual Basic) is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.
 
Back
Top