Visual Basic - 向电子邮件发送回车符

发布于 2024-08-22 08:18:32 字数 401 浏览 8 评论 0原文

如何将包含换行符(回车符)的字符串发送到电子邮件?

问题是我将字符串发送到电子邮件,而电子邮件删除了回车符。

Dim myApp As New Process

    emailStringBuilder.Append("mailto:")

    emailStringBuilder.Append("&subject=" & tmpID & " - " & subject)

    emailStringBuilder.Append("&body= " & msgStringBuilder.ToString)

    myApp = System.Diagnostics.Process.Start(emailStringBuilder.ToString)

How can I send a string to an email with new lines (carriage returns) included?

The problem is that I am sending the string to an email, and the email strips out the carriage returns.

Dim myApp As New Process

    emailStringBuilder.Append("mailto:")

    emailStringBuilder.Append("&subject=" & tmpID & " - " & subject)

    emailStringBuilder.Append("&body= " & msgStringBuilder.ToString)

    myApp = System.Diagnostics.Process.Start(emailStringBuilder.ToString)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-08-29 08:18:32

我猜您正在向 StringBuilder 添加不带换行符的内容,因为我刚刚测试过它并且它对我来说效果很好。

Imports System.Text
Module Module1
    Sub Main()
        Dim sb As New StringBuilder
        sb.AppendLine("Line 1")
        sb.AppendLine("Line 2")
        sb.Append("Line 3" & vbCrLf)
        sb.Append("Line 4 without CRLF")
        sb.Append("Line 5")
        Console.WriteLine(sb.ToString)
    End Sub
End Module

对于上面的代码我得到以下输出

<前><代码>第 1 行
2号线
3号线
没有 CRLF 的第 4 行第 5 行

希望这有帮助。

编辑

好的,所以根据新信息(关于电子邮件),上述内容仍然成立。如果您使用 .Append 添加到字符串生成器,您将丢失,或者更正确地说,看不到换行符。相反,您必须使用 .AppendLine 它将所有重要的 CR 和 LF 代码添加到字符串的末尾。

但是,我有点困惑你如何发送电子邮件。我以前在网页上见过这种方式,但从未在 vb.net 上见过。以这种方式发送电子邮件几乎肯定会迫使您发送没有换行符的电子邮件!

我可以建议您查看 Microsoft 的以下内容,了解如何使用 System.Web.Mail 命名空间从 Visual Basic 发送电子邮件。这并不难,您可以更好地控制通过这种方式发送的电子邮件...

Microsoft示例

I'm guessing you're adding to your StringBuilder without newlines as I've just tested it and it works fine for me.

Imports System.Text
Module Module1
    Sub Main()
        Dim sb As New StringBuilder
        sb.AppendLine("Line 1")
        sb.AppendLine("Line 2")
        sb.Append("Line 3" & vbCrLf)
        sb.Append("Line 4 without CRLF")
        sb.Append("Line 5")
        Console.WriteLine(sb.ToString)
    End Sub
End Module

For the above code I get the following output

Line 1
Line 2
Line 3
Line 4 without CRLFLine 5

Hope this helps.

EDIT

Ok, so based on the new information (about the email) the above still holds true. If you add to a stringbuilder with .Append you will lose, or more correctly, not see newlines. Instead you must use .AppendLine which will add the all important CR and LF codes onto the end of your string.

However, I am a little confused how you are sending email. I've seen it done this way from a webpage before but never from vb.net. Sending an email this way will almost certainly force you to send an email without newlines!!

Can I suggest you look at the the following from Microsoft on how to send email from Visual Basic using the System.Web.Mail namespace. It's not hard and you'll get a lot more control over the email you send this way....

Microsoft example

虐人心 2024-08-29 08:18:32

对于任何也在寻找可行的发送到电子邮件的东西的人,您想要按照 @CResults 答案中的方式进行操作,但在每一行添加 "%0A" 。这将使其传达给大多数电子邮件客户端。

To anyone also looking for something that will work went sent to email, you want to do as in @CResults answer, but add "%0A" to each line. This will get it through to most email clients.

你的背包 2024-08-29 08:18:32

您还可以通过发送正文为 HTML 格式的电子邮件来实现此目的,如下所示:

当邮件正文为 HTML 格式时,请在字符串中添加
标记。如果正文采用 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 格式,则使用 vbCrLfvbNewLine

Dim mail As New MailMessage
mail.Body = "First Line" & vbCrLf
mail.Body += "Second Line" & vbNewLine
mail.Body += "Third Line"

You could also achieve this by sending the email with the body in HTML format like so:

When the body of the message is in HTML format, add the <br> tags right in your String. vbCrLf and StringBuilder don't work if the body is in HTML format.

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, you use vbCrLf or vbNewLine:

Dim mail As New MailMessage
mail.Body = "First Line" & vbCrLf
mail.Body += "Second Line" & vbNewLine
mail.Body += "Third Line"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文