.NET 中的 MailMessage:为什么 <br/> 标签会按字面意思插入到我的电子邮件中?
MailMessage in .NET: why are the <br/> tags being inserted literally into my emails?
我有这个邮件发送功能:
''' <summary>
''' Sends an email.
''' </summary>
''' <param name="from">Who is the email coming from?</param>
''' <param name="to">Who is the email going to?</param>
''' <param name="subject">Subject of the email.</param>
''' <param name="body">Body text of the email.</param>
''' <returns>true if an email was sent, false if sending failed.</returns>
Public Shared Function SendEmail(ByVal from As String, ByVal [to] As String, ByVal subject As String, ByVal body As String) As Boolean
' sanity check - can't send to nobody!
If String.IsNullOrWhiteSpace([to])
Return False
End If
' get http objects
Dim http = HttpContext.Current
Dim Request = http.Request
Dim Session = http.Session
Dim Response = http.Response
' find SMTP server
Dim smtpServer As String
If Request.ServerVariables("SERVER_NAME") = OverridableServerName Then
smtpServer = LocalServerName
Else
smtpServer = Request.ServerVariables("SERVER_NAME")
End If
' compose email
Try
Dim message As New MailMessage(from, [to], subject, body.Replace(vbCrLf, "<br/>"))
message.IsBodyHtml = True
Dim emailClient As New SmtpClient(smtpServer)
emailClient.Send(message)
Return True
Catch ex As Exception
Dim uri = New Uri(Request.Url.PathAndQuery, UriKind.Relative).ToString()
' trim off the query string
If (uri.IndexOf("?") >= 0)
uri = uri.Substring(0, uri.IndexOf("?"))
End If
Response.Redirect("ErrorPage.asp?ErrorMsg=From " & uri & "<br><br>" & smtpServer & "<br>" & from & "<br>" & [to] & "<br>" & subject & "<br>" & Replace(body, vbCrLf, "<br>") & "<br>" & Replace(ex.Message, vbCrLf, "<br>"))
Return False
End Try
End Function
问题是,即使我将 IsBodyHtml
标志设置为 true,我要替换换行符的 <br/>
标记也会按字面意思呈现,而不是作为生成的电子邮件中的换行符。有一个更好的方法吗?我不应该用 <br/>
标签替换换行符吗? (我会测试它,但我没有本地电子邮件服务器,这就是为什么我要问在这里做什么,所以我不必为了看到结果而不断地将它扔过栅栏进入测试区域电子邮件...)
message.IsBodyHtml = True
确实修复了它,尽管我认为它没有;当我们认为系统未修复时,我们只是在测试错误的系统。糟糕!
我有这个邮件发送功能:
''' <summary>
''' Sends an email.
''' </summary>
''' <param name="from">Who is the email coming from?</param>
''' <param name="to">Who is the email going to?</param>
''' <param name="subject">Subject of the email.</param>
''' <param name="body">Body text of the email.</param>
''' <returns>true if an email was sent, false if sending failed.</returns>
Public Shared Function SendEmail(ByVal from As String, ByVal [to] As String, ByVal subject As String, ByVal body As String) As Boolean
' sanity check - can't send to nobody!
If String.IsNullOrWhiteSpace([to])
Return False
End If
' get http objects
Dim http = HttpContext.Current
Dim Request = http.Request
Dim Session = http.Session
Dim Response = http.Response
' find SMTP server
Dim smtpServer As String
If Request.ServerVariables("SERVER_NAME") = OverridableServerName Then
smtpServer = LocalServerName
Else
smtpServer = Request.ServerVariables("SERVER_NAME")
End If
' compose email
Try
Dim message As New MailMessage(from, [to], subject, body.Replace(vbCrLf, "<br/>"))
message.IsBodyHtml = True
Dim emailClient As New SmtpClient(smtpServer)
emailClient.Send(message)
Return True
Catch ex As Exception
Dim uri = New Uri(Request.Url.PathAndQuery, UriKind.Relative).ToString()
' trim off the query string
If (uri.IndexOf("?") >= 0)
uri = uri.Substring(0, uri.IndexOf("?"))
End If
Response.Redirect("ErrorPage.asp?ErrorMsg=From " & uri & "<br><br>" & smtpServer & "<br>" & from & "<br>" & [to] & "<br>" & subject & "<br>" & Replace(body, vbCrLf, "<br>") & "<br>" & Replace(ex.Message, vbCrLf, "<br>"))
Return False
End Try
End Function
问题是,即使我将 IsBodyHtml
标志设置为 true,我要替换换行符的 <br/>
标记也会按字面意思呈现,而不是作为生成的电子邮件中的换行符。有一个更好的方法吗?我不应该用 <br/>
标签替换换行符吗? (我会测试它,但我没有本地电子邮件服务器,这就是为什么我要问在这里做什么,所以我不必为了看到结果而不断地将它扔过栅栏进入测试区域电子邮件...)
message.IsBodyHtml = True
确实修复了它,尽管我认为它没有;当我们认为系统未修复时,我们只是在测试错误的系统。糟糕!