-1

I'm using a string builder to create a body message while trying to do something. the issue es I'm getting the value from the string builder and this one is formatted. Example from my string builder:

Following files were attached by email: 
1. C:\SALASFRI2_20150824094158_ScrubLog.txt - Record Count: 8
2. C:\SALASFRI2_20150824102328_ScrubLog.txt - Record Count: 8
3. C:\SALASFRI2_20150824102516_ScrubLog.txt - Record Count: 8
4. C:\SALASFRI2_20160121125353_ScrubLog.txt - Record Count: 8
5. C:\SALASFRI2_20160121125659_ScrubLog.txt - Record Count: 8

===================================================

Total Files: 5

Please contact your system administrator for any further assitance.

That's exactly how the stringbuilder looks, but when the email arrived, it lose their format as follow:

enter image description here

do you know if there is a property to don't lose the format when it arrive to my email?

This is my code:

            if (Notification.IncludeFileName || Notification.IncludeRecordCount)
            {
                BodyMessage.Append("Following files were attached by email: \n");

                for (int index = 0; index < NumberOfFiles; index++)
                {
                   if (Notification.IncludeFileName && Notification.IncludeRecordCount)
                        BodyMessage.Append((index + 1) + ". " + LocalFiles[index] + " - Record Count: " + File.ReadLines(LocalFiles[index]).Count() + "\n");                           
                  else                       
                        BodyMessage.Append((index + 1) + ". " + LocalFiles[index] + File.ReadLines(LocalFiles[index]).Count() + "\n");
                }
            }             

            if (Notification.IncludeNumberOfFiles)
            {
                BodyMessage.Append("\n===================================================\n\n");
                BodyMessage.Append("Total Files: " + NumberOfFiles + "\n");
            }

            BodyMessage.Append("\nPlease contact your system administrator for any further assitance.");                
            ////////////////////END OF SUBJECT AND BODY MAIL MESSAGE VALIDATION

            ////////............SEND THE ATTACHMENTS THROUGH EMAIL................///////////
            try
            {
                MailMessage Email = new MailMessage();
                SmtpClient smtp = new SmtpClient(AppConfiguration.SMTPServer);

                Email.From = new MailAddress(AppConfiguration.EmailAddress);
                Email.To.Add(protocol.SMTPEmailList);
                Email.Subject = EmailSubject;                            
                Email.Body = BodyMessage.ToString();                    

                if (directories.Zip)
                    Email.Attachments.Add(new Attachment(ZipName));
                else
                    foreach (var attachment in LocalFiles)
                    {
                        Email.Attachments.Add(new Attachment(attachment));
                    }

                smtp.Port = AppConfiguration.SmtpPort;
                smtp.UseDefaultCredentials = true;
                smtp.Send(Email);
                Email.Dispose();
Javier Salas
  • 1,064
  • 5
  • 15
  • 38

1 Answers1

0

You probably need to use "\r\n" as your newline sequence instead of just "\n".

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Then the problem is that the receiving client is rendering it wrong. – jstedfast Apr 06 '16 at 21:54
  • I found that the issue only is happening with this line: BodyMessage.Append((index + 1) + ". " + LocalFiles[index] + " - Record Count: " + File.ReadLines(LocalFiles[index]).Count() + "\n"); With the else line that is not happening and is formatting well – Javier Salas Apr 06 '16 at 21:57
  • Try adding a space before the \n – jstedfast Apr 06 '16 at 22:20
  • For some reason I had to add like 10 spaces to work. not sure what's the reason get this work. – Javier Salas Apr 06 '16 at 23:03