function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lapaullapaul 

Sending a long text email issue in Apex

Hi,

I have a problem sending a long text email using Apex code. Basically I want to send the following text (Msg).

 

       Msg = 'Date Test Taken: ' + Datetime.now().format('MM/dd/yyyy hh:mm a');
        Msg = Msg + ' The following user has just passed the test.';
        Msg = Msg + ' User Name: ' + uname;
        Msg = Msg + ' RN License#: ' + RNlicense;
        Msg = Msg + ' Branch Location: ' + Location;

However it does not like this Msg variable in Apex command mail.setPlainTextBody(Msg);
This will work as long as there is text inside the single quote mail.setPlainTextBody('Date Test Taken: ');
Is there a workaround to send a long text in Apex? see the sendmail method below. Please advice. Thanks

 

  public void SendMail(){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] email = new String[]
{'lapaul@crescenthh.com'};

 

    email.add('lapaul@yahoo.com');
    email.add(
'paul@hotmail.com');
   
    mail.setToAddresses(email);
 
    mail.setReplyTo(
'lapaul@crescenthh.com');
    mail.setSenderDisplayName('Online Tests Admin');
    mail.setSubject('Antimicrobial Resistance, MRSA and other MDRO test');
    mail.setPlainTextBody('Date Test Taken: ' + Datetime.now().format('MM/dd/yyyy hh:mm a'));
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }   

 

Pradeep_NavatarPradeep_Navatar

Find below a workable sample code. It works fine for the long text :

 

 

                                                public class sendEmail1

                                                {

                                                                public String subject { get; set; }

                                                                public String body { get; set; }

                                                                private final Account account;

                                                                // Create a constructor that populates the Account object

                                                                public Account getAccount()

                                                                {

                                                                   return account;

                                                                }

                                                                public PageReference send()

                                                                {

                                                                                // Define the email

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

                                                                                String addresses;

                                                                                String Msg = 'Date Test Taken: ' + Datetime.now().format('MM/dd/yyyy hh:mm a');

                                                                                Msg = Msg + body;

                                                                                if (account.Contacts != null)

                                                                                {

                                                                                                addresses = account.Contacts[0].Email;

                                                                                                // There may be more contacts, so loop through the whole list

                                                                                                for (Integer i = 1; i < account.Contacts.size(); i++)

                                                                                                {

                                                                                                                if (account.Contacts[i].Email != null)

                                                                                                                {

                                                                                                                addresses += ':' + addresses;

                                                                                                                }

                                                                                                }

                                                                                }

                                                                                String[] toAddresses = addresses.split(':', 0);

                                                                                // Sets the paramaters of the email

                                                                                email.setSubject( subject );

                                                                                email.setToAddresses( toAddresses );

                                                                                email.setPlainTextBody(Msg);

                                                                                return page.testconfirm;

                                                                }

                                                }

 

Did this answer your question? if so, please mark it solved.