7

我可以使用此代码在我的 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

但是如何在正文中添加多行?

4

6 回答 6

9

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

StringBuilder在这里很有用:

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

mail.Body = sb.ToString()
于 2012-04-23T15:23:25.313 回答
4

我会为您的正文创建一个变量,然后将其添加到 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

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

于 2012-04-23T15:25:37.123 回答
4

如果您的邮件正文需要采用 HTML 格式,<br>请在您的字符串中添加标签。vbCrLf如果正文是 HTML 格式,StringBuilder则不工作。

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

如果不是 HTML 格式,这里的其他答案似乎很好。

于 2016-05-12T20:50:23.587 回答
1

像这样?

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

然后

mail.Body = myMessage
于 2012-04-23T15:25:27.530 回答
1

尝试system.environment.newline在字符串中...应该可以

于 2012-04-23T15:30:48.433 回答
0

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

strBody = "This is for testing SMTP mail from me<BR> line 2" 
于 2020-08-14T19:03:35.513 回答