AppDude27
AppDude27

Reputation: 77

VB.NET Adding New Lines with SMTP Email

I'm using VB.NET to send an email. An example of the email might look like this:

Name: (Name)
Email Address: (Email Address)
Phone Number: (Phone Number)

The following code snippet below works. It just puts too much spacing between lines. I've tried getting rid of System.Environment.NewLine but then my lines don't format properly anymore.

I also tried not using a StringBuilder and it also didn't format properly as well.

'Email Generation ----------------------------------------------------
 Dim builder As New StringBuilder
 builder.AppendLine("Name: " + Name + System.Environment.NewLine")
 builder.AppendLine("Email Address: " + EmailAddress+ System.Environment.NewLine")
 builder.AppendLine("Phone Number: " + PhoneNumber+ System.Environment.NewLine")

 emailBody = builder.ToString

Any suggestions would definitely help!

Thanks!

Upvotes: 0

Views: 475

Answers (2)

Karen Payne
Karen Payne

Reputation: 5117

If using html format rather than plain text try the following

Dim fullName As String = "Karen Payne"
Dim emailAddress As String = "[email protected]"
Dim phoneNumber As String = "555-334-1111"
Dim content =
    <body>
        <table>
            <tr>
                <td>Name: <%= fullName %></td>
            </tr>
            <tr>
                <td>Email address: <%= emailAddress %></td>
            </tr>
            <tr>
                <td>Phone number: <%= phoneNumber %></td>
            </tr>
        </table>
    </body>

If this still has too much space for you let me know.

Upvotes: 1

obl
obl

Reputation: 1799

Make sure you have the encoding right, and if you're not using < br > then IsBodyHtml needs to be set to false. Here's a simple example:

Dim server As New SmtpClient
Dim email As New MailMessage
server.UseDefaultCredentials = False
server.Credentials = New System.Net.NetworkCredential("x", "x")
server.Port = 25
server.EnableSsl = False
server.Host = "x"

email.From = New MailAddress("x")
email.To.Add("x")
email.BodyEncoding = System.Text.Encoding.UTF8
Dim s = "Name: " + vbNewLine + "Email: " + vbNewLine + "Phone"
email.Body = s
server.Send(email)

I replaced all the credentials/addresses with "x"

Upvotes: 1

Related Questions