How do I send formatted text in email using C#?

17,474

Solution 1

HI all,

Thanks for all the help you people gave.

I got the answer in the following link

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

Basically the replace function will give the answer.

The complete answer is as below:


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

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

            //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;          

Solution 2

You probably need to convert the newlines to proper HTML breaks:

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

Solution 3

For the HTML version of the email you will need to replace linebreaks with <br /> tags. A simple string.Replace should do this.

For the plaintext email I'm going to guess your email is formatted as you need and you're using Outlook to receive the email.

Outlook helpfully removes what it deems to be additional whitespace (which tends to be any whitespace). There's an option to turn it off, usually given at the top of the window when you open the message fully.

To switch it off for Outlook entirely:

Tools > Options > Preferences > E-mail Options... > Uncheck Remove extra line breaks in plain text messages

Share:
17,474
Srikanth V M
Author by

Srikanth V M

Programmer

Updated on June 26, 2022

Comments

  • Srikanth V M
    Srikanth V M almost 2 years

    Below is my code snippet to send 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);
            }
    
        }
    

    The message body contains the text with line breaks.

    But I am unable to format the text. In the mail it appears as a continuous line with space replacing the line breaks.

    How can I make it work as expected?

    • PassionateDeveloper
      PassionateDeveloper over 14 years
      So in the textBox6 you have linebreakes and when you send the email the linebreaks are replaced with space, did I understnad it right?
    • Bijendra Singh
      Bijendra Singh over 14 years
      In that code you're sending the content of textBox6 - so without knowing what you're putting in its kind of hard to see what should come out. If sending HTML formatted mail you're going to need to use <p></p> and <br />
    • Stefan Steinegger
      Stefan Steinegger over 14 years
      You probably don't care, but everyone can see the password of your mail server now ...
    • Srikanth V M
      Srikanth V M over 14 years
      Ya Murph you are right but how do i send the data. Do i need to use some kind of loop to send the data. But ultimately we need to send the message in a single shot using smtp.mail(mail)
  • Srikanth V M
    Srikanth V M over 14 years
    No i dont use Outlook. But i'll try the replace option
  • Srikanth V M
    Srikanth V M over 14 years
    Hi thanks for the post. But this is not working. I think there is no wrong in logic.
  • Srikanth V M
    Srikanth V M over 14 years
    Hi The concept worked when i did text.Replace("\r\n","<br />")