How to insert line break in email message using Apex in Salesforce?

To insert line break in email message using Apex in Salesforce, we have to use <br> tag. setHtmlBody() method is used here to send the email in HTML format.

Sample Trigger:

trigger memberInviteNotify on Member__c (after insert,after update)
{
    for(Member__c member:trigger.New)
    {
        String[] toAddresses = new String[] {member.E_Mail_Id__c};
        String messageBody;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        
        //Email invitation
        if(trigger.isInsert)
        {
            mail.setSubject('Welcome to Sweet 16 Siebel Batch');
            messageBody = '<html><body>Hi ' + member.Name + ',Welcome to Sweet 16</body></html>';
            mail.setHtmlBody(messageBody);   
        }
        
        //Email notification
        if(trigger.isUpdate)
        {
            mail.setSubject('Updates in your details');

            //Message with line break tag
            messageBody = '<html><body>Hi ' + member.Name + ',Changes have been made to your details. <br><br>Contact administrator if you are not responisble.</body></html>';

            mail.setHtmlBody(messageBody);
        }
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Cheers!!!

No comments:

Post a Comment