在SMTP电子邮件VB.NET的主体中添加多行

时间:2012-04-23 15:21:22

标签: vb.net smtp

我可以使用此代码在我的Exchange服务器上发送电子邮件

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("myemail@email.com")
        mail.To.Add("otheremail@email.com")
        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

但是如何在身体上添加多条线?

6 个答案:

答案 0 :(得分:8)

只需将其视为普通文本对象,您可以在句子之间使用Environment.NewLinevbNewLine

StringBuilder在这里很有用:

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

mail.Body = sb.ToString()

答案 1 :(得分:3)

我会为你的身体创建一个变量然后将它添加到mail.Body中,所以它看起来像这样。

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("myemail@email.com")
    mail.To.Add("otheremail@email.com")
    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

这将附加换行符,你应该在电子邮件中拥有它自己的每一行。

答案 2 :(得分:2)

如果邮件正文需要采用HTML格式,请在字符串中添加<br>标记。如果正文为HTML格式,则vbCrLfStringBuilder无法正常工作。

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

如果它不是HTML格式,那么其他答案似乎都很好。

答案 3 :(得分:1)

喜欢这个吗?

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

然后

mail.Body = myMessage

答案 4 :(得分:1)

尝试字符串中的system.environment.newline ...应该正常工作

答案 5 :(得分:0)

对我有用的是在字符串中使用

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