Sending emails from Apex isn’t complicated, but most developers are overwhelmed with the process.
However, APEX provides classes for composing email messages. Let us walk you through a practical, clean implementation of how to send an email from APEX.
Understanding How Apex Handles Email
Salesforce sends emails through the Messaging class. The workhorse here is Messaging.SingleEmailMessage. This class can help you send a single email message to one or more people.
The setToAddresses() funtion set the To field of the email. You can provide one email address or multiple addresses as input, separating them with a semicolon.
Next, the setCcAddresses() function sets the CC field of the email. On the other hand, the setInReplyTo() function differs in that it takes a string as an argument.
Furthermore, the setSubject() function sets the e-mail subject, and thesetHtmlBody() sets the e-mail body in HTML form.
But if you want to set the body in text format, then use the function setPlainTextBody(). Once configured, use the sendEmail() function and Salesforce handles delivery.
Basic Apex Code to Send an Email
This sends a plain-text email to a direct address.
public class emailSending {
public string toMail { get; set;}
public string ccMail { get; set;}
public string repMail { get; set;}
public void sendMail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string[] to = new string[] {toMail};
string[] cc = new string[] {ccMail};
email.setToAddresses(to);
if(ccMail!=null && ccMail != '')
email.setCcAddresses(cc);
if(repmail!=null && repmail!= '')
email.setInReplyTo(repMail);
email.setSubject('Test Mail');
email.setHtmlBody('Hello, <br/><br/>This is the test mail that you generated. <br/>The Email Id for which this mail was generated by '+toMail+'<br/><br/>Regards<br/> Developer');
try{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}catch(exception e){
apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
}
toMail = '';
ccMail = '';
repMail = '';
}
}
VF Page Code
The Code for my VF page is something like this:
<apex:page controller="emailSending">
<p>
Test Email Sending:
</p>
<apex:form>
<apex:outputLabel value="Send Mail To: " for="To"/>
<apex:inputText value="{!toMail}" id="To"/><br/>
<apex:outputLabel value="CC Mail To: " for="CC"/>
<apex:inputText value="{!ccMail}" id="CC"/><br/>
<apex:outputLabel value="Reply Mail To: " for="rep"/>
<apex:inputText value="{!repMail}" id="rep"/><br/>
<apex:commandButton action="{!sendMail}" value="Send Email"/>
</apex:form>
</apex:page>
Output
The live preview for my page can be seen here:
http://wk-yat-demo-developer-edition.ap4.force.com/wkDemo/testGenerateMail
A Practical Note on Apex Email Limits
This is the part most tutorials skip.
Salesforce enforces daily email limits when you hit them; your code still runs, but messages don’t leave the org.
Apex email is powerful, but it’s not an unlimited infrastructure. If you’re building automated notifications at scale, design with limits in mind:
- Avoid sending emails inside loops
- Consolidate where possible
- Log email attempts for visibility
Final Thoughts
Sending email with Apex is straightforward, but you have to write email logic that survives real-world scenarios.
Furthermore, you have to keep the implementation clean, respect limits, separate UI from logic, and test with the real user scenarios.
When used properly, it’s one of the most reliable tools in the Salesforce stack.
Support
If you have any further queries related to the Salesforce stack, please email us at [email protected].
You can also hire Salesforce Developers to help your business deliver more by providing expertise and resources for all kinds of Salesforce development projects.
2 comments
You can contact to the developer forum: https://developer.salesforce.com/forums