Solved

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

Posted on 2006-10-25
4
181 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:cbdrako99
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Xeavn
ID: 17807798
Try using VBCRLF

So

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

etc.

0
 

Author Comment

by:cbdrako99
ID: 17807937
Thanks,  I tried it but the result is unchanged.
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 125 total points
ID: 17808474
try using environment.newline instead of VBCRLF. if thats not working, maybe you are sending in HTML format, in which case try adding "<br/>" or enclose the paragraphs in "<p></p>" codes.

msg.body = "First Name:" + " " + txtFirstName.Text.Trim() + "<br/> " + txtLastName.Text.Trim() + ...

or

msg.body = "<p>First Name:" + " " + txtFirstName.Text.Trim() + "</p> <p>" + txtLastName.Text.Trim() + "</p>...
0
 
LVL 4

Expert Comment

by:Xeavn
ID: 17812584
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question