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

时间:2009-11-02 12:09:19

标签: 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);
        }

    }

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

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

如何让它按预期工作?

3 个答案:

答案 0 :(得分:5)

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

text.Replace("\n", "<br/>")

答案 1 :(得分:1)

对于电子邮件的HTML版本,您需要使用<br />标记替换换行符。一个简单的string.Replace应该这样做。

对于明文电子邮件,我猜你的电子邮件是按照你的需要进行格式化的,而你正在使用Outlook接收电子邮件。

Outlook有助于删除它认为是额外的空格(往往是任何空格)。有一个选项可以关闭它,通常在你完全打开信息时在窗口顶部给出。

完全关闭Outlook:

工具&gt; 选项&gt; 偏好设置&gt; 电子邮件选项 ...&gt;取消选中删除纯文本邮件中的额外换行符

答案 2 :(得分:0)

全部,

感谢您提供的所有帮助。

我在以下链接中得到了答案

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23552772.html?sfQueryTermInfo=1+bodi+c+email+format+send+while

  
    

基本上,替换功能会给出答案。

         

完整的答案如下:

  

            //The text will be loaded here
            string s2= textBox6.Text;     

            //All blank spaces would be replaced for html subsitute of blank space(&nbsp;) 
            s2 = s2.Replace(" ", "&nbsp;");          

            //Carriage return & newline replaced to <br/>
            s2=s2.Replace("\r\n", "<br/>");                
            string Str = "<html>";
            Str += "<head>";
            Str += "<title></title>";
            Str += "</head>";
            Str += "<body>";
            Str += "<table border=0 width=95% cellpadding=0 cellspacing=0>";
            Str += "<tr>";
            Str += "<td>" + s2 + "</td>";
            Str += "</tr>";
            Str += "</table>";
            Str += "</body>";
            Str += "</html>";                        
            mail.Subject = textBox4.Text;                          
            mail.Body = Str;