Jeff Cox
Jeff Cox

Reputation: 333

Adding Newline in string for HTML email body

I'm trying to generate an email with some basic formatting based on labels in a FormView. I'm going the route of Process.Start("mailto:... instead of System.Net.Mail so the email opens up in the default email client to give the user a chance to edit To: CC: etc without making a new form just to handle that. I've got the following code-behind to handle an "Email Form" button for emailing the URL of the webform.

protected void fvBF_ItemCommand(object sender, FormViewCommandEventArgs e)
{
    if (e.CommandName == "Email")
    {
        Label lblBFID = (Label)fvBookingForm.FindControl("lblBFID");
        Label lblProjectID = (Label)fvBookingForm.FindControl("lblProjectNum");
        Label lblProjectName = (Label)fvBookingForm.FindControl("lblProjectName");
        Label lblReleaseName = (Label)fvBookingForm.FindControl("lblReleaseName");
        Label lblPMName = (Label)fvBookingForm.FindControl("lblPM");

        String strReleaseName = String.IsNullOrEmpty(lblReleaseName.Text) ? "[Description]" : lblReleaseName.Text;

        String pmFirst = lblPMName.Text.ToString().Split()[0];
        String pmLast = lblPMName.Text.ToString().Split()[1];

        String strSubject = "BF " + lblBFID.Text + " - " + lblProjectName.Text  + " - Release " + strReleaseName;
        String strBody = "A Booking Form for Project #"+ lblProjectID.Text + " - " + lblProjectName.Text + 
            " - Release " + strReleaseName + " has been created or modified. \n\n" + Request.Url.ToString();

        Process.Start("mailto:" + pmFirst + "." + pmLast + "@company.com?subject=" +
            HttpUtility.HtmlAttributeEncode(strSubject) + "&body=" +
            HttpUtility.HtmlAttributeEncode(strBody).Replace("\n", Environment.NewLine));
    }
}

However, when the email is generated, there are no line breaks in the body between the "A Booking Form...." sentence and the URL. I've tried putting Environment.NewLine directly in the string.

...created or modified. " + Environment.Newline + Environment.NewLine + Request.Url.ToString();

Which basically gives me the same results. I've tried replacing the \n with <br /> which doesn't add the line break and for some reason, doesn't display the URL either. I can only guess that the problem has to do with the HtmlAttributeEncode() and getting it to parse the NewLine properly. Is there something I'm missing here?

Upvotes: 1

Views: 5094

Answers (3)

amigatra
amigatra

Reputation: 1

Model.Message = "my message \n second message";

then add this style to string tag style="white-space: pre-line;" example <h3 style="white-space: pre-line;">@Model.Message</h3>

Upvotes: 0

RonC
RonC

Reputation: 33791

You might want to try .Replace("\r\n", "<br />") on the body after you have done your encoding of the body.

Upvotes: 2

Thaddeus Winker
Thaddeus Winker

Reputation: 1

You should probably use StringBuilder here instead of String.

You can then do the following:

StringBuilder builder = new StringBuilder();
builder.AppendLine(string.Format("A Booking Form for Project #{0} - {1}",lblProjectID.Text, lblProjectName.Text));
builder.AppendLine(string.Format(" - Release {0}  has been created or modified.", strReleaseName));
builder.AppendLine();
builder.AppendLine(Request.Url.ToString());
String strBody = builder.ToString();

You can also include (char)10 and (char)13 in your string creation. e.g.:

string.Format("First Line{0}{1}Second Line", (char)10, (char)13);

Upvotes: 0

Related Questions