2012-04-23 74 views
5

的身体,我可以使用此代码到我的Exchange服务器添加多行SMTP电子邮件VB.NET

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 

但我怎么可以添加多行到身体上发送电子邮件?

+0

用多行准备字符串消息并将其添加到body属性中时出现什么问题? – Steve 2012-04-23 15:23:52

回答

8

只要把它当作正常的文本对象,你可以在句子之间使用Environment.NewLinevbNewLine

StringBuilder在这里很有用:

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

mail.Body = sb.ToString() 
1

喜欢这个?

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

然后

mail.Body = myMessage 
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("[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 

这将追加换行符,并且您应该在电子邮件中包含每行。

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格式,这里的其他答案似乎是好的。