{"id":2384,"date":"2026-10-18T13:00:00","date_gmt":"2026-10-18T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/"},"modified":"2026-11-15T11:30:13","modified_gmt":"2026-11-15T06:00:13","slug":"sending-email-from-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/","title":{"rendered":"Sending Email from Apex with SingleEmailMessage and Templates"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sending Email from Apex gives you full control over who gets a message, what it says and when it goes out. A scheduled job can email every parent whose fee is overdue, with the student name and amount merged in. So you automate messages that no point-and-click tool quite covers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/lightning-email-templates-letterheads\/\">Lightning Email Templates<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/organization-wide-email-addresses-deliverability\/\">org-wide email addresses<\/a>. We will build a fee reminder for Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Sending Email from Apex?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sending Email from Apex means using the <code>Messaging<\/code> class to build and send messages in code. You create a <code>SingleEmailMessage<\/code>, set its fields, then pass it to <code>Messaging.sendEmail<\/code>. There is also <code>MassEmailMessage<\/code> for many recipients at once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of writing a letter by hand versus using a printing press. A flow or alert is the handwritten letter for one simple case. Apex is the press, where you decide the paper, the wording and how many go out.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Sending Email from Apex Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Flows and workflow alerts cover most simple cases. However, they struggle when the recipient list is calculated, when you need an attachment built in code, or when one job must email hundreds of different people with different totals.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sending Email from Apex handles all three. It also gives you the result of each send, so you can log failures instead of guessing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Sending Email from Apex Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sending Email from Apex follows the same five steps every time, whatever the content.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-1-1024x640.png\" alt=\"Sending Email from Apex diagram showing the five steps of one send\" class=\"wp-image-2382\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The five steps of one Apex email send.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">A Basic Message<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();\nmail.setToAddresses(new List&lt;String&gt;{ 'parent@example.com' });\nmail.setSubject('Fee reminder');\nmail.setPlainTextBody('Your fee of 5000 is due on Friday.');\nMessaging.sendEmail(new List&lt;Messaging.SingleEmailMessage&gt;{ mail });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sendEmail<\/code> always takes a List, even for one message. That is because sending in bulk is the cheaper path.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reading the Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sending Email from Apex returns a result per message, which you should check.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Messaging.SendEmailResult&gt; results =\n    Messaging.sendEmail(new List&lt;Messaging.SingleEmailMessage&gt;{ mail }, false);\n\nfor (Messaging.SendEmailResult r : results) {\n    if (!r.isSuccess()) {\n        System.debug('Failed: ' + r.getErrors()[0].getMessage());\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second argument is <code>allOrNone<\/code>. Passing <code>false<\/code> lets good messages go even if one fails.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using a Template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Merge fields only fill when you set a target object, because Salesforce needs a record to merge from.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EmailTemplate t = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Fee_Reminder' LIMIT 1];\n\nMessaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();\nmail.setTemplateId(t.Id);\nmail.setTargetObjectId(contactId);     \/\/ who the merge fields come from\nmail.setWhatId(enrolmentId);           \/\/ the related record for merge fields\nmail.setSaveAsActivity(true);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without <code>setTargetObjectId<\/code>, the template sends but every merge field comes out blank.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sending From the Right Address<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default the email shows the running user. An org-wide address makes it come from the company.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OrgWideEmailAddress owa = [SELECT Id FROM OrgWideEmailAddress\n                           WHERE Address = 'support@brightpath.example' LIMIT 1];\nmail.setOrgWideEmailAddressId(owa.Id);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Adding an Attachment<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();\natt.setFileName('receipt.txt');\natt.setBody(Blob.valueOf('Receipt for 5000'));\nmail.setFileAttachments(new List&lt;Messaging.EmailFileAttachment&gt;{ att });<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Key Methods for Sending Email from Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table lists the methods you will use most.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>What it sets<\/th><\/tr><\/thead><tbody><tr><td>setToAddresses<\/td><td>Plain email addresses<\/td><\/tr><tr><td>setTargetObjectId<\/td><td>Contact, lead or user for merge fields<\/td><\/tr><tr><td>setWhatId<\/td><td>Related record for template merges<\/td><\/tr><tr><td>setTemplateId<\/td><td>The email template to use<\/td><\/tr><tr><td>setSubject and setPlainTextBody<\/td><td>Content without a template<\/td><\/tr><tr><td>setOrgWideEmailAddressId<\/td><td>The From address<\/td><\/tr><tr><td>setSaveAsActivity<\/td><td>Logs the email on the record<\/td><\/tr><tr><td>setFileAttachments<\/td><td>Files to send<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">setTargetObjectId vs setToAddresses<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These two decide who receives the email, and they behave differently.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>setTargetObjectId<\/th><th>setToAddresses<\/th><\/tr><\/thead><tbody><tr><td>Recipient<\/td><td>A record<\/td><td>Any address<\/td><\/tr><tr><td>Merge fields<\/td><td>Work<\/td><td>Do not fill<\/td><\/tr><tr><td>Activity logging<\/td><td>Possible<\/td><td>Not available<\/td><\/tr><tr><td>Daily limit<\/td><td>Often exempt when logged<\/td><td>Counts<\/td><\/tr><tr><td>Bounce handling<\/td><td>Tracked on the record<\/td><td>Not tracked<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-2-1024x640.png\" alt=\"Sending Email from Apex comparison of target object and plain addresses\" class=\"wp-image-2383\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Target object compared with plain addresses when Sending Email from Apex.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use a target object when you email a real contact. Use addresses for internal alerts to a fixed inbox.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Sending Email from Apex Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path wants a nightly job that emails every parent whose fee is overdue, using one approved template.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Build the Service<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class FeeReminderService {\n    public static void sendReminders() {\n        List&lt;Enrolment__c&gt; due = [\n            SELECT Id, Student__c, Amount_Due__c\n            FROM Enrolment__c\n            WHERE Fee_Status__c = 'Overdue' AND Student__c != null\n            LIMIT 500\n        ];\n        if (due.isEmpty()) { return; }\n\n        EmailTemplate t = [SELECT Id FROM EmailTemplate\n                           WHERE DeveloperName = 'Fee_Reminder' LIMIT 1];\n        OrgWideEmailAddress owa = [SELECT Id FROM OrgWideEmailAddress LIMIT 1];\n\n        List&lt;Messaging.SingleEmailMessage&gt; batch = new List&lt;Messaging.SingleEmailMessage&gt;();\n        for (Enrolment__c e : due) {\n            Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();\n            m.setTemplateId(t.Id);\n            m.setTargetObjectId(e.Student__c);\n            m.setWhatId(e.Id);\n            m.setOrgWideEmailAddressId(owa.Id);\n            m.setSaveAsActivity(true);\n            batch.add(m);\n        }\n        Messaging.sendEmail(batch, false);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All the messages go in one <code>sendEmail<\/code> call, which counts as a single email invocation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Test Without Real Sends<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tests never deliver mail, so you assert on the invocation count instead.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class FeeReminderServiceTest {\n    @isTest static void sendsOneBatch() {\n        Integer before = Limits.getEmailInvocations();\n        Test.startTest();\n        FeeReminderService.sendReminders();\n        Test.stopTest();\n        System.assert(Limits.getEmailInvocations() &gt;= before, 'Should attempt a send');\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Watch the Limits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These are the limits that matter when Sending Email from Apex.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Limit<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>Email invocations per transaction<\/td><td>10<\/td><\/tr><tr><td>Recipients per SingleEmailMessage<\/td><td>100 total across to, cc and bcc<\/td><\/tr><tr><td>External single emails per day<\/td><td>5,000 per org on most editions<\/td><\/tr><tr><td>Mass email recipients per day<\/td><td>Edition dependent<\/td><\/tr><tr><td>Emails in a test context<\/td><td>Never actually delivered<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Sending Email from Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calling sendEmail inside a loop, which burns the 10 invocation limit.<\/li>\n\n<li>Forgetting setTargetObjectId, so template merge fields come out blank.<\/li>\n\n<li>Using allOrNone true, so one bad address blocks every other message.<\/li>\n\n<li>Sending without an org-wide address, so mail appears from the running user.<\/li>\n\n<li>Ignoring the daily limit, which silently stops later sends.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Sending Email from Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build a List of messages and send once per transaction.<\/li>\n\n<li>Use templates so marketing controls the wording, not your code.<\/li>\n\n<li>Set an org-wide address for anything customer facing.<\/li>\n\n<li>Pass false for allOrNone and log the failures.<\/li>\n\n<li>Check Limits.getEmailInvocations in long jobs before sending again.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a method that emails a single contact using a template and logs the send as an activity. Hint: you need both setTemplateId and setTargetObjectId.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Sending Email from Apex Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Sending Email from Apex questions come up in integration and automation interviews.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Sending Email from Apex Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: Which class sends email in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sending Email from Apex uses the Messaging class, with SingleEmailMessage for individual sends or MassEmailMessage for bulk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: How do you send a message once it is built?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call Messaging.sendEmail and pass a List of messages, even if there is only one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What does setToAddresses do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It sets plain email addresses as recipients, so there is no record behind them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What does setTargetObjectId do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It names the contact, lead or user that merge fields read from.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Why would merge fields come out blank?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because no target object was set, so there is no record to merge from.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What is setWhatId used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It names the related record, such as an opportunity, that a template can merge from.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What does setSaveAsActivity do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It logs the email as an activity on the recipient&#8217;s record.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Are emails really sent in tests?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Tests never deliver mail, so you assert on limits or the code path instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Sending Email from Apex Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How many email invocations are allowed per transaction?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ten. So build a List and send once rather than looping.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How many recipients can one SingleEmailMessage have?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Up to 100 across the to, cc and bcc fields combined.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: What is an org-wide email address used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It sets a shared From address, so mail appears from the company rather than the running user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: What does the allOrNone parameter control?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whether one failure rolls back the whole send. Passing false lets the good messages go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you attach a file?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Build an EmailFileAttachment with a file name and a Blob body, then set it on the message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: What is the difference between SingleEmailMessage and MassEmailMessage?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SingleEmailMessage sends one message with full control. MassEmailMessage targets many records with one template and fewer options.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: Can Apex send email to an address outside your org?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but it counts against the daily external email limit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How do you check whether a send succeeded?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read the SendEmailResult list, calling isSuccess and getErrors on each result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Sending Email from Apex Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: a batch job must email 10,000 parents. How do you design it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use Batch Apex with a scope of about 100, build one List per batch and send once in each execute. Then the invocation limit is never reached.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Why is the daily email limit a deployment risk?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because it is per org, not per job. So a test data load can exhaust it and silently stop production mail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you keep email wording out of Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use email templates and reference them by DeveloperName, so admins change wording without a release.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Scenario: emails send but never arrive. What do you check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Deliverability settings, whether the org is in System email only mode, bounce status on the contact and the daily limit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you handle bounced addresses?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Turn on bounce management, then check the bounce fields before sending and skip those records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Can you send email from a trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can, but it is risky. Better to queue the work with a Queueable so the save is not slowed or rolled back by an email failure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: How do you test template merges without sending?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call Messaging.renderStoredEmailTemplate, which returns the merged subject and body for inspection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: What governs email in a scratch or sandbox org?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Email deliverability is often set to No access or System email only, so no mail leaves until an admin changes it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Does Sending Email from Apex work in a Flow?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Flow has its own Send Email action for simple cases. However, an invocable method is the usual way to reuse Apex email logic from a Flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Can you schedule Sending Email from Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A Schedulable class can call your email service nightly, which is how most reminder jobs run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: How do you avoid emailing the same person twice?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Track what you sent, usually with a date field or a log record. Then filter those records out of the next run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: Scenario: the daily email limit is hit at noon. What happens?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Later sends fail with a limit error for the rest of the day. So large jobs should run at night and check the limit first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: Is Sending Email from Apex the right choice for marketing blasts?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Marketing Cloud or Account Engagement handle consent, tracking and volume far better, so keep Apex for transactional mail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_namespace_Messaging.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: Messaging Namespace<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_classes_email_outbound_single.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: SingleEmailMessage Class<\/a><\/li>\n\n<li><a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=sf.emailadmin_deliverability.htm&amp;type=5\" target=\"_blank\" rel=\"noopener noreferrer\">Salesforce Help: Email Deliverability<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sending Email from Apex uses the Messaging class and SingleEmailMessage.<\/li>\n\n<li>Always pass a List to sendEmail, even for one message.<\/li>\n\n<li>setTargetObjectId is what makes template merge fields fill.<\/li>\n\n<li>Ten email invocations per transaction, so send in bulk.<\/li>\n\n<li>Tests never deliver mail, so assert on limits instead.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we receive mail instead of sending it, with <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/\">Inbound Email Services in Apex<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sending Email from Apex gives you full control over who gets a message, what it says and when it goes out. A scheduled job&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2381,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[239,8,96],"tags":[98,31,26],"swapz_series":[],"class_list":["post-2384","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apex-fundamentals","category-salesforce","category-salesforce-developer","tag-apex","tag-email","tag-intermediate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sending Email from Apex: Messaging Class Guide<\/title>\n<meta name=\"description\" content=\"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sending Email from Apex: Messaging Class Guide\" \/>\n<meta property=\"og:description\" content=\"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-18T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:00:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Sending Email from Apex with SingleEmailMessage and Templates\",\"datePublished\":\"2026-10-18T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/\"},\"wordCount\":1621,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sending-email-from-apex-featured.png\",\"keywords\":[\"Apex\",\"Email\",\"Intermediate\"],\"articleSection\":[\"Apex Fundamentals\",\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/\",\"name\":\"Sending Email from Apex: Messaging Class Guide\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sending-email-from-apex-featured.png\",\"datePublished\":\"2026-10-18T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:13+00:00\",\"description\":\"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sending-email-from-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sending-email-from-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Sending Email from Apex featured image: code editor card with email message\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/sending-email-from-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sending Email from Apex with SingleEmailMessage and Templates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sending Email from Apex: Messaging Class Guide","description":"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/","og_locale":"en_US","og_type":"article","og_title":"Sending Email from Apex: Messaging Class Guide","og_description":"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-18T07:30:00+00:00","article_modified_time":"2026-11-15T06:00:13+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Sending Email from Apex with SingleEmailMessage and Templates","datePublished":"2026-10-18T07:30:00+00:00","dateModified":"2026-11-15T06:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/"},"wordCount":1621,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png","keywords":["Apex","Email","Intermediate"],"articleSection":["Apex Fundamentals","Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/","name":"Sending Email from Apex: Messaging Class Guide","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png","datePublished":"2026-10-18T07:30:00+00:00","dateModified":"2026-11-15T06:00:13+00:00","description":"Learn Sending Email from Apex with SingleEmailMessage, templates, attachments, org-wide addresses and limits, plus the most asked interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/sending-email-from-apex-featured.png","width":2400,"height":1260,"caption":"Sending Email from Apex featured image: code editor card with email message"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Sending Email from Apex with SingleEmailMessage and Templates"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2384"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2384\/revisions"}],"predecessor-version":[{"id":3204,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2384\/revisions\/3204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2381"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2384"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}