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

How to send email notification from Apex code


How do you send an email notification from Apex code? Please advice.





Be aware there are governor limits related to sending emails in Apex (10 emails per transaction).


Sending emails in triggers usually will exceed that limit when batches are posted.  The workaround for that is to do DML in Apex that causes a Workflow to fire the email instead.


Thanks for the tips. Do you have any sample codes for using DML to make workflow to fire email?






Here's a copy and paste from some of my stuff to get you started....  It only work for plain text.  I do html too, but html emails are a little more complicated as you have to do DML then scheduled apex to send them 10 at a time if you want to avoid the governor limit.



Object: Apex Outbound Email

Evaluation Criteria: Every time a record is created or edited

Rule Criteria: Apex Outbound Email: Sent equals False

Workflow Actions: Email Alert (Send the Email) and Field Update (Sent to True)


Email Alert:

Recipients: Email Field: To

Object: Apex Outbound Email


Email Template:

Subject: {!Apex_Outbound_Email__c.Subject__c}

Body: {!Apex_Outbound_Email__c.Body__c}



List<Apex_Outbound_Email__c> e_list = new List<Apex_Outbound_Email__c>();

Apex_Outbound_Email__c e = new Apex_Outbound_Email__c();
e.Subject__c='Standard 36 Hour Quote Follow Up';
e.Body__c='New Task\n\nSomeone has assigned you the following new task:\n\nSubject: Standard 36 Hour Quote Follow Up\nPriority: Normal\nQuote: ';

insert e_list;






I copied exactly the outbound email Apex sample code from the link you provided. However I got an error message that says "Error: Compile Error: unexpected token: ')' at line 17 column 31" which it referenced this line mail.setToAddresses(toAddresses);.
Any ideas why it throw out that error? Please advice. thanks

// Create a new single email message object  
// that will send out a single email to the addresses in the To, CC & BCC list.  
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.  
String[] toAddresses = new String[] {''}; 
String[] ccAddresses = new String[] {''};

// Assign the addresses for the To and CC lists to the mail object.  

// Specify the address used when the recipients reply to the email.   

// Specify the name used as the display name.  
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.  
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.  

// Optionally append the email signature to the email.  
// The email address of the user executing the Apex Code will be used.  

// Specify the text content of the email.  
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
     ' View case <a href='+case.Id+'>click here</a>');

// Send the email you have created.  
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });