Line break into email message

Asked

Viewed 4,786 times

2

In my string mensagem, i would like to have a two line space, I tried using "/n" and I did not get the expected result.

@{
    var customerName = Request["customerName"];
    var customerEmail = Request["customerEmail"];
    var customerRequest = Request["customerRequest"];
    var customerSubject = Request["customerSubject"];
    var atendimento = "[email protected]";
    var errorMessage = string.Empty;
    var debuggingFlag = false;
    string mensagem = string.Format(customerRequest + "\n\n{0}", customerEmail);
    try
    {
        // Initialize WebMail helper
        WebMail.SmtpServer = "smtp.teste.com.br";
        WebMail.SmtpPort = 25;
        WebMail.UserName = "[email protected]";
        WebMail.Password = "teste";
        WebMail.From = "[email protected]";

        // Send email
            WebMail.Send(to: atendimento,
                subject: customerSubject + " - " + customerName,
                body: mensagem
            );
    }
    catch (Exception ex)
    {
        errorMessage = ex.Message;
    }
}
  • tries to use \n\r, your message is text/plain? If it’s text/html you can use the tag <br/>.

  • 1

    @claudsan the Octeto used should be \r\n.

3 answers

3


Use Environment.NewLine instead of \n. It is correct. I am considering that you are using the default for MailMessage.IsBodyHtml. Otherwise the solution is to put a <br/>.

Do not capture Exception. See more on the subject by following all the answers I’ve given starting with that answer.

  • The Enviroment.Newline did not serve, because it is the same thing as "/r/n" if I am mistaken, but the <br/> served, I read your answer that indicated me, very good, I will avoid using Exceptions, and I will take the habit of asking the question "Why use Exception here?"

  • In the link that has there links to other things, it is important to read everything, although it is quite something is very important. Today the exception capture is the most poorly used resource in languages. As you will see there capture Exception has its place, only it is not in this. Note that I did not say that you should not capture any exception (also I am not saying that you should). If you want to capture, you can be sure to do something useful with it and capture the most specific exception possible. If <br\> that worked the configuration was marked to accept HTML.

  • Thanks a lot @bigown...

1

Use "Environment.Newline" concatenated with the rest of the message!

  • 3

    Hi Tiago, it’s cool that you want to contribute to the site, but in this case, in addition to the answer not being prepared, repeats what has already been said in the 2 answers given more than 2 years ago. But you can [Dit] and try to add some relevant detail not observed in the others, if you feel that is the case. It would be nice to have [Tour] and [Help] the time you have a little time.

1

In C# use the Environment.NewLine sort of like this:

String mensagem = String.format(customerRequest + Environment.NewLine + Environment.NewLine + "{0}", customerEmail);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.