Trigger Email in Salesforce Apex

One of the coolest things about Salesforce is that you can send emails programmatically right from your Apex code. This is super useful for things like alerts, notifications, or even follow-ups.

In this post, we’ll walk through a simple method that accepts an email address as input and sends a predefined email to that address. If you’re new to Apex or just starting out with Salesforce automation, don’t worry we’ll break everything down so it’s easy to understand and follow.

Let’s get started!

🧠 What Are We Trying to Do?

 

Imagine a scenario where your Salesforce org needs to send a quick alert to a user or customer  maybe someone just submitted a form, or their case was updated.

Instead of manually writing and sending emails, we can create an Apex method that:

  • Accepts an email address as input
  • Builds an email message
  • Sends it automatically through Salesforce

That’s exactly what the method below does.

🔍 Code Explanation

 
public with sharing class ApexUseCaseThree {
    public static void sendAlertToCustomer(String emailAddress){

We start by declaring a class named ApexUseCaseThree and inside it, we define a static method called sendAlertToCustomer. This method takes one input  the recipient’s email address.

        if(!String.isBlank(emailAddress)){

Before proceeding, we check if the email address is not blank. This is important because we don’t want to attempt sending an email without a valid address  it would cause errors.

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

Here, we create a new email object using Salesforce’s built-in Messaging.SingleEmailMessage class. This object holds all the details about the email we’re about to send.

            List<String> toAddressList = new List<String>{emailAddress};

We prepare a list containing the recipient’s email. Salesforce methods expect email addresses in a list format — even if you’re only sending to one person.

            mail.setSubject('URGENT - Take a look!');
            mail.setPlainTextBody('This is a notification triggered by Salesforce via Apex for you!');
            mail.setToAddresses(toAddressList);

We set:

  • The subject line of the email

  • The body text

  • The recipient list

These are the core components of a basic email.

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

Finally, we call Messaging.sendEmail() and pass in our email object inside a list. This sends the email to the specified recipient.

✅ Why This Is Useful

 

This method is great for sending alerts from Apex  like notifying users when a record is updated or when an error occurs. You can even plug this into a trigger or call it from a Flow (via Apex invokable methods with slight modifications).

It’s reusable, lightweight, and demonstrates how powerful Apex is when working with Salesforce automation.

🧾 Final Code Snippet

 
public with sharing class ApexUseCaseThree {
    public static void sendAlertToCustomer(String emailAddress){
        if(!String.isBlank(emailAddress)){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            List<String> toAddressList = new List<String>{emailAddress};
            mail.setSubject('URGENT - Take a look!');
            mail.setPlainTextBody('This is a notification triggered by Salesforce via Apex for you!');
            mail.setToAddresses(toAddressList);
            
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}

🎥 Watch It in Action!

Prefer learning by watching? I’ve got you covered! Here’s a quick video walkthrough showing how this method works in a real Salesforce org.

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)