top of page

How to Send email from Apex class.

Updated: Aug 28, 2020

This is most common question most of the developer ask, How to send emails from apex class? , so here in this blog we are going to see that.

Step 1. Go to your name -> Developer Console ->File -> New -> Apex Class

Step 2: Create one method and copy all the mentioned code into your method.

list<Messaging.SingleEmailMessage> mlist =new List<Messaging.SingleEmailMessage>(); 

Define List of To Address

String [] Addresses=new String[0]; 

Define List of To Address

String [] CCAddresses=new String[0];
Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();

If you wanted send email without classic email templates you can use following code to set subject and body.

email.setSubject('Quote From Cloudwaale');
email.setPlainTextBody('Please find attached quotation');

Target Object can only be user, Lead and Contact.


In case of User we have to use following method


Replace template id from your org


If you have any organization wide addresses you can sen that as from email

for(OrgWideEmailAddress owd:[select id,address,displayname from OrgWideEmailAddress])

If you not set any from email id system will take login users id as default from email .

To send attachment

List<Messaging.EmailFileAttachment> efaList=new List<Messaging.EmailFileAttachment>();
Messaging.EmailFileAttachment efa=new Messaging.EmailFileAttachment();

pdfBody you have to fetch it from attachment object

 efa.body=pdfBody; //Blob

This will send email and store result in result list.

Messaging.SendEmailResult [] result=Messaging.sendEmail(mlist);

Steps 3: As per comment inline do the changes in you code and run the code.


That's it, we have created code to send email from apex class. To get the latest update and answer for all how question related to Salesforce, customization, lightning, Apex, Visulaforce Page,REST and SOAP API and Tips and tricks please follow Cloudwaale.

Thank you for visiting Cloudwaale, Please like and comment your view on this post.

Note: This Blog is Perspective of Author and not of Salesforce. This blog is only for study purpose.

6,054 views0 comments

Recent Posts

See All


bottom of page