Play Games

Search This Blog

Tuesday, March 11, 2025

Send an email using apex class salesforce

 

Solution: Use sendEmail method as shown in the example below

public class EmailController {
    public static void sendEmail() {
        List<String> lstToAddress = new List<String>();
        lstToAddress.add(UserInfo.getUserEmail()); //Add email to whom email needs to be sent
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(lstToAddress);
        email.setSubject('Test Email'); // Give subject of the Email
        String body='Email Content'; // content of the email
        email.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
        System.debug('Email Sent');
    }
}

Call the method from developer console using below code
EmailController.sendEmail();

Output:



No comments:

Post a Comment