-1

I am very very new to Csharp. I am editing a file parsing application a coworker wrote before he left. I am trying to send an HTML formatted email that's supposed to alert users when the app runs into an exception:

catch (Exception ex)
    {
        Logger.Error(ex.Message);  //63-116

        string fromemail = "sampleem@samp.com";
        string toemail = "sampleem@samp.com";
        string subject = "Automated From Repair Cruncher";
        string team = "Data Engineering Team";
        string body = "The following error occured in Client.Handler.EntityHandler.cs while Parser was processing:\n";
        body += ex.Message;
        var result = EmailUtil.Emailalert(fromemail, toemail, subject, team, body);
    }

However, when I get the email, there is no newline between the processing and the ex.message. It does it on the same line. Why is this? I also tried \n but it didn't give me newline either

I have tried Environment.Newline but it doesn't work either

edo101
  • 629
  • 6
  • 17
  • Are you sure it's actually missing? `\n` should work just fine, I wonder if your email client is trying to render it as HTML or rich text and ignoring the newline. – superstator Nov 19 '20 at 03:43
  • @superstator Yes it is actually trying to send an HTML email. The string is fed into an HTML string that displays it as an HTML email. I did try to use Environement.Newline and it is not working. I assume it's the HTML causing the issue? If so, what is a remedy? – edo101 Nov 19 '20 at 03:46
  • 5
    Insert HTML formatting, like `
    ` or `

    `

    – superstator Nov 19 '20 at 03:47
  • 2
    I would suggest using `ex.ToString()` instead of `ex.Message` since the most valuable part is often the stack trace, and not just the message. – ProgrammingLlama Nov 19 '20 at 03:51
  • @John so ex.ToString() gives you a more verbose alert? I will test it. Never seen this before either. I just got thrown into this as an intern with very little .net knowledge – edo101 Nov 19 '20 at 03:52
  • @superstator Thanks that worked. I had similar code in python and I never needed to do that. I wonder why in C# I need to use an HTML tag to create the space. Python just did it with '\n' – edo101 Nov 19 '20 at 03:53
  • I would go with @superstator, HTML doesn't care about `\n`, in face if you create an html document and just write hello on two different lines they will apear inline. Use
    .
    – Jerry Nov 19 '20 at 03:53
  • 1
    It has nothing to do with C#, it's being formatted through HTML – Jerry Nov 19 '20 at 03:54
  • 1
    `Python just did it with '\n'` If that is true (which is hard to verify since you haven't provided code) then Python is likely converting `\n` to `
    `.
    – mjwills Nov 19 '20 at 03:56
  • 3
    Yes, or your Python app was outputting plain text email instead of html – Jerry Nov 19 '20 at 03:58
  • @mjwills I will check back in with Python code just to make sure I am not talking out of my butt. But yes, I used the Python logic which is why I never bothered with
    . The python logic would print things exactly as I formatted the string in Python with no HTML tags. The HTML tags only came into play with making tables. Not pure strings. Interesting to see the difference. I have A LOT to learn about .net
    – edo101 Nov 19 '20 at 03:59
  • 4
    Again - this is likely not a C# vs Python issue. You are sending a HTML email. So if you need a line break you need to generate a line break _in HTML_. – mjwills Nov 19 '20 at 04:00

1 Answers1

-5

string text = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";

text = text.Replace("@", "@" + System.Environment.NewLine);

Adding a newline into a string in C#

Please refer this link. Thanks.

BRG
  • 1