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 

How to send email notification from Apex code

Hi,

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

 

thanks

 

Rick_NookIndRick_NookInd

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.

lapaullapaul

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

 

Regards,

Paul

 

TbomTbom
Rick_NookIndRick_NookInd

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.

 

Workflow:

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}

 

Apex:

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

Apex_Outbound_Email__c e = new Apex_Outbound_Email__c();
e.To__c=opp_map3.get(t.OwnerId);
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: ';
e_list.add(e);

insert e_list;

 

 

lapaullapaul

 

 

Hi,
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[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};
  

// Assign the addresses for the To and CC lists to the mail object.  
    
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email.   
    
mail.setReplyTo('support@acme.com');

// 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.  
    
mail.setBccSender(false);

// Optionally append the salesforce.com email signature to the email.  
    
// The email address of the user executing the Apex Code will be used.  
    
mail.setUseSignature(false);

// 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=https://na1.salesforce.com/'+case.Id+'>click here</a>');

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