Pickle
Pickle

Reputation: 1124

Adding multiple lines to body of SMTP email VB.NET

I can use this code to send an email on my Exchange server

Try
        Dim SmtpServer As New SmtpClient
        Dim mail As New MailMessage
        SmtpServer.Credentials = New Net.NetworkCredential()
        SmtpServer.Port = 25
        SmtpServer.Host = "email.host.com"
        mail = New MailMessage
        mail.From = New MailAddress("[email protected]")
        mail.To.Add("[email protected]")
        mail.Subject = "Equipment Request"
        mail.Body = "This is for testing SMTP mail from me" 


        SmtpServer.Send(mail)

    catch ex As Exception
        MsgBox(ex.ToString)
    End Try

But how can I add multiple lines to the body?

Upvotes: 8

Views: 30515

Answers (6)

Diego
Diego

Reputation: 2372

What it works for me is using
in the string..

strBody = "This is for testing SMTP mail from me<BR> line 2" 

Upvotes: 0

Tony L.
Tony L.

Reputation: 19496

If the body of your message needs to be in HTML format, add the <br> tags right in your String. vbCrLf and StringBuilder don't work if the body is in HTML format.

Dim mail As New MailMessage
mail.IsBodyHtml = True
mail.Body = "First Line<br>"
mail.Body += "Second Line<br>"
mail.Body += "Third Line"

If it is not in HTML format, the other answers here appear to be good.

Upvotes: 4

bebadbutneverbesad
bebadbutneverbesad

Reputation: 21

try the system.environment.newline in the the string ... should work

Upvotes: 1

Robert
Robert

Reputation: 3074

I would create a variable for your body and then add that to the mail.Body so it would look something like this.

Try
    Dim strBody as string = ""
    Dim SmtpServer As New SmtpClient
    Dim mail As New MailMessage
    SmtpServer.Credentials = New Net.NetworkCredential()
    SmtpServer.Port = 25
    SmtpServer.Host = "email.host.com"
    mail = New MailMessage
    mail.From = New MailAddress("[email protected]")
    mail.To.Add("[email protected]")
    mail.Subject = "Equipment Request"
    strBody = "This is for testing SMTP mail from me" & vbCrLf
    strBody += "line 2" & vbCrLf
    mail.Body = strBody

    SmtpServer.Send(mail)

catch ex As Exception
    MsgBox(ex.ToString)
End Try

That will append the line breaks and you should have each line on it's own in the email.

Upvotes: 4

Steve
Steve

Reputation: 216363

Like this?

Dim myMessage as String = "This is for testing SMTP mail from me" + Environment.NewLine
myMessage = myMessage + "Line1" + Environment.NewLine

then

mail.Body = myMessage

Upvotes: 1

LarsTech
LarsTech

Reputation: 81675

Just treat it like a normal text object where you can use Environment.NewLine or vbNewLine between sentences.

StringBuilder is useful here:

Dim sb As New StringBuilder
sb.AppendLine("Line One")
sb.AppendLine("Line Two")

mail.Body = sb.ToString()

Upvotes: 10

Related Questions