将多行添加到 SMTP 电子邮件 VB.NET 的正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10283400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding multiple lines to body of SMTP email VB.NET
提问by Pickle
I can use this code to send an email on my Exchange server
我可以使用此代码在我的 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("[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?
但是如何在正文中添加多条线?
回答by LarsTech
Just treat it like a normal text object where you can use Environment.NewLine
or vbNewLine
between sentences.
把它当作一个普通的文本对象,你可以在其中使用Environment.NewLine
或vbNewLine
在句子之间使用。
StringBuilder
is useful here:
StringBuilder
在这里很有用:
Dim sb As New StringBuilder
sb.AppendLine("Line One")
sb.AppendLine("Line Two")
mail.Body = sb.ToString()
回答by Tony L.
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.
如果您的消息正文需要采用 HTML 格式,请<br>
在您的字符串中添加标签。如果正文是 HTML 格式vbCrLf
,StringBuilder
则不起作用。
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.
如果它不是 HTML 格式,这里的其他答案似乎很好。
回答by Robert
I would create a variable for your body and then add that to the mail.Body so it would look something like this.
我会为您的正文创建一个变量,然后将其添加到 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("[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.
这将附加换行符,并且您应该在电子邮件中拥有自己的每一行。
回答by bebadbutneverbesad
try the system.environment.newline
in the the string ... should work
尝试system.environment.newline
在字符串中......应该可以工作
回答by Steve
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