asp.netemailblogengine.netblog-engine

How to Insert Line Breaks When Emailing?


Neither the "\n" or "<br />" are working. I'm trying to insert line breaks when emailing the contents of a custom form:

protected void SubmitBtn_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        StringBuilder mailBody = new StringBuilder();
        mailBody.Append("Company Name: " + companyText.Text.Trim() + "\n\n");
        mailBody.Append("Name: " + firstNameText.Text.Trim() + " " 
            + lastNameText.Text.Trim() + "\n\n");
        mailBody.Append("Email: " + emailText.Text.Trim() + "\n\n");

        if(!String.IsNullOrEmpty(tradeText.Text))
            mailBody.Append("Trade: " + tradeText.Text.Trim() + "\n\n");

            bool success = SendEmail(emailText.Text.Trim(), 
                companyText.Text.Trim(), "SubContractors Form Submission",
                    mailBody.ToString()); 
    }
}

private bool SendEmail(string email, string name, 
    string subject, string message)
{
    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress(BlogSettings.Instance.Email, name);
        mail.ReplyTo = new MailAddress(email, name);

        mail.To.Add(BlogSettings.Instance.Email);
        mail.Subject = subject;
        mail.Body += message;

        if (Utils.SendMailMessage(mail).Length > 0)
        {
            return false;
        };
    }
    return true;
}

UPDATE:

I got <br /> working eventually. Reason it didn't work was because this was the original code I got from the Contact form:

mail.Body += message.Replace("\n\n", "<br />");

MailMessage.IsBodyHtml is set to true in the core library code.


Solution

  • You can use: