如何在VB .NET窗体中格式化邮件正文 [英] How to format the mail body in VB .NET windows form
问题描述
我有2个文本框供用户输入邮件主题和邮件正文。
当他输入标题和正文并保存时他们将存储在数据库。
当他想发送电子邮件然后他将从数据库中检索标题和正文并发送电子邮件
一切正常,我们可以将邮件发送给用户。
但是当用户进入时下面的bodylike并保存在数据库中它保存在字符串数据类型中
欢迎用户
感谢您选择我们
我们很高兴有你
谢谢
当我们从数据库中检索尸体并发送邮件然后邮件如下所示
欢迎用户感谢您选择我们我们很高兴有您的感谢
无论如何都要格式化主体并发送,因为用户输入的是texbox
提前感谢
我尝试过:
I have 2 text boxes for the user to enter the mail subject and mail body.
when he enters the title and body and save they will store in the database.
when he wants to send an email then he will retrieve the title and body from the database and sends an email
Everything is working and we can able to send the mail to the user.
But when the user enters the bodylike below and save in the database it is saved in the string datatype
welcome to user
thank you for choosing us
we are glad to have you
thanks
when we retrieve the body from the database and sent the mail then the mail is like below
welcome to user thank you for choosing us we are glad to have you thanks
is there anyway to format the body and sent as it is the user enters in the texbox
thanks in advance
What I have tried:
Dim Fromemail As String = dt.Rows(0)("email").ToString()
Dim password As String = dt.Rows(0)("password").ToString()
Dim mailsubject As String = dt.Rows(0)("title").ToString()
Dim mailbody As String = dt.Rows(0)("body").ToString()
Dim portno As Integer = dt.Rows(0)("portno")
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress(Fromemail)
mail.To.Add(New MailAddress(Toemail))
mail.Subject = mailsubject
mail.Body = mailbody
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", portno)
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential(Fromemail, password)
Try
client.Send(mail)
MessageBox.Show("Email Sent Successfully")
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
推荐答案
这里的关键是
The key here is
mail.IsBodyHtml = True
HTML需要标签进行格式化,它不尊重CRLF字符,>
您可以尝试
HTML needs tags for formatting, it doesn't respect CRLF characters,
You could try
mail.Body = mailbody.Replace("\n", "<br />");
或类似。
or similar.
这篇关于如何在VB .NET窗体中格式化邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!