Visual Basic - 向电子邮件发送回车符
如何将包含换行符(回车符)的字符串发送到电子邮件?
问题是我将字符串发送到电子邮件,而电子邮件删除了回车符。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您正在向 StringBuilder 添加不带换行符的内容,因为我刚刚测试过它并且它对我来说效果很好。
对于上面的代码我得到以下输出
希望这有帮助。
编辑
好的,所以根据新信息(关于电子邮件),上述内容仍然成立。如果您使用
.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.
For the above code I get the following output
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
对于任何也在寻找可行的发送到电子邮件的东西的人,您想要按照 @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.您还可以通过发送正文为 HTML 格式的电子邮件来实现此目的,如下所示:
当邮件正文为 HTML 格式时,请在字符串中添加
标记。如果正文采用 HTML 格式,则vbCrLf
和StringBuilder
不起作用。如果不是 HTML 格式,则使用
vbCrLf
或vbNewLine
: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
andStringBuilder
don't work if the body is in HTML format.If it is not in HTML format, you use
vbCrLf
orvbNewLine
: