Link to home
Start Free TrialLog in
Avatar of cbdrako99
cbdrako99Flag for United States of America

asked on

How to insert a line feed when emailing from a web form using Visual Basic

Hi,
I have a web form which collects data from various text fields and sends it via SMTP to a single recipient. The fields are all in the from msg.Body.  Everything works fine but the resultant email is difficult to read because all of information in the email ends up on one or two lines. How can I insert line breaks so that the resultant email is more easily read?
Thanks in advance...

Sample VB Code behind the "Send" button.

Imports System.Net.Mail

Partial Class Registration
    Inherits System.Web.UI.Page

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim fromAddress As New MailAddress("sender@someplace.com")
        Dim toAddress As New MailAddress("recipient@anotherplace.com")
        Dim msg As New MailMessage(fromAddress, toAddress)
        msg.Subject = " Test MAIL MESSAGE"
        msg.Body = "First Name:" + " " + txtFirstName.Text.Trim() + " " + _
        txtLastName.Text.Trim() + ", " + txtMI.Text.Trim() _
        "Address:" + " " + txtAddress + "City:" + " " + txtCity.Text _
' More code like above
        msg.IsBodyHtml = True
        Dim mailSender As New System.Net.Mail.SmtpClient
        mailSender.Host = "mail.hostname.com"
        Try
            mailSender.Send(msg)
            lblMessage.Text = "Message Sent!"
        Catch ex As Exception
            lblMessage.Text = ex.Message
        End Try
Avatar of Xeavn
Xeavn

Try using VBCRLF

So

msg.body = "First Name:" + " " + txtFirstName.Text.Trim() + VBCRLF + txtLastName.Text.Trim() + ...

etc.

Avatar of cbdrako99

ASKER

Thanks,  I tried it but the result is unchanged.
ASKER CERTIFIED SOLUTION
Avatar of newyuppie
newyuppie
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or if you perfer to send text mail try removing msg.IsBodyHtml = True, and replace it with

msg.BodyEncoding = Encoding.ASCII
msg.BodyFormat = MailFormat.Text