Solved

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

Posted on 2006-10-25
4
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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