2

I know this question is asked before and could found in the below links, (I am posting this as I want to post my code with some changes to the solutions provided in the below links.)

PlainText email doesn't line break as expected

Send Email with the body formatted

I found the following code in a mentioned links above and tried though it doesn't work, What am I doing wrong here?

String plainTextBody = '';
plainTextBody += 'Confirmed Late Delivery is changed to unticked \n';
plainTextBody += 'Delivered Status is changed to a value that is not Delivered Late \n';
plainTextBody += 'Late Code is changed \n';
plainTextBody += 'or Late Code Subcategory is changed \n\n';

String[] toRecipients = new List<String> {'hasantha@com.au'};
String[] ccRecipients = new String[] {};
String[] bccRecipients = new String[] {};
String subject = 'test sub';
String body = plainTextBody;

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

Messaging.reserveSingleEmailCapacity(1);
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
email.setbccAddresses(bccRecipients);
email.setSubject(subject);
email.setPlainTextBody(body);

System.debug(LoggingLevel.INFO,'** entered sendEmail, to:' + toRecipients + ' cc:' + ccRecipients + ' bcc:' + bccRecipients + ' subject:' + subject + ' body:' + body);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Below is the content of the received email

Confirmed Late Delivery is changed to unticked Delivered Status is changed to a value that is not Delivered Late Late Code is changed or Late Code Subcategory is changed

Thanks in-advance!

  • 1
    just now tested above code. line breaking working for me. – Ratan Paul Dec 9 '15 at 4:01
  • It's weird this doen't work for me. I ran this through anonymous apex run. – Hasantha Dec 9 '15 at 4:05
  • I replaced the first 5 lines with String plainTextBody = ''; plainTextBody += ' Confirmed Late Delivery is changed to unticked \n '; plainTextBody += ' Delivered Status is changed to a value that is not Delivered Late \n '; plainTextBody += ' Late Code is changed \n '; plainTextBody += ' or Late Code Subcategory is changed \n\n '; AND it works! Do not know the exact reason. It doen't make sense of giving space(s) between each value assignments allowing the new line character to be functioned. Still a question for me! – Hasantha Dec 9 '15 at 4:14
  • Can you pls add your exact code which are giving issue ? I am not able to replicate the issue by giving spaces. – Ratan Paul Dec 9 '15 at 4:51
  • 1
    no idea why this is not working. I will look into this in my free time. If I will found something will let you know. – Ratan Paul Dec 9 '15 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.