如何使用C#在电子邮件中发送格式化文本?

Sri*_*V M 1 c# email

以下是我发送电子邮件的代码段:


            MySqlCommand cmdsd;
            MySqlConnection conn;
            string s23 = "";
            conn = new MySqlConnection("server=localhost;database=projecttt;uid=root;password=techsoft");
            conn.Open();

             //smtp which will be loaded is webmail.techsofttechnologies.com
            cmdsd = new MySqlCommand("select smtp from smtp", conn);
            MySqlDataReader dr45 = cmdsd.ExecuteReader();

            while (dr45.Read())
            {
                s23 = dr45.GetString(0).Trim();
            }
            string s1 = textBox3.Text;
            string s4 = textBox1.Text;
            string S5 = textBox2.Text;
            string attachment = textBox5.Text;
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(s4, S5);
            mail.BodyEncoding = Encoding.UTF8;                
            mail.To.Add(s1);
            mail.Subject = textBox4.Text;
            mail.Body = "<body>"+textBox6.Text+"</body>";                
            //mail.Body = textBox6.AppendText("\n");

            AlternateView planview = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable tby those clients that don't support html");
            AlternateView htmlview = AlternateView.CreateAlternateViewFromString("<b>This is bold text and viewable by those mail clients that support html<b>");
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            System.Net.Mail.Attachment jil = new System.Net.Mail.Attachment(attachment);
            mail.Attachments.Add(jil);
            SmtpClient smtp = new SmtpClient(s23);
            try
            {
                smtp.Send(mail);
            }

            catch (Exception ex)
            {
                Exception exc = ex;
                string Message = string.Empty;
                while (exc != null)
                {
                    Message += exc.ToString();
                    exc = exc.InnerException;
                }
            }
            conn.Close();
            this.Close();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }

    }
Run Code Online (Sandbox Code Playgroud)

邮件正文包含带换行符的文本.

但我无法格式化文本.在邮件中,它显示为一个连续的行,空格代替换行符.

如何让它按预期工作?

Per*_*ahl 5

您可能需要将换行符转换为正确的HTML中断:

text.Replace("\n", "<br/>")
Run Code Online (Sandbox Code Playgroud)