{"id":2388,"date":"2026-10-19T09:00:00","date_gmt":"2026-10-19T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/"},"modified":"2026-11-15T11:30:09","modified_gmt":"2026-11-15T06:00:09","slug":"apex-inbound-email-services","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/","title":{"rendered":"Inbound Email Services in Salesforce with Apex Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Inbound Email Services let Salesforce receive an email and run your Apex code on it. A parent emails an enquiry address, and your class reads the subject, pulls the student name out of the body and creates a Lead. So email becomes a data entry channel without anyone retyping it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/sending-email-from-apex\/\">Sending Email from Apex<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-case-rules-assignment-escalation\/\">Email-to-Case<\/a>. We will handle course enquiries for Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Inbound Email Services?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inbound Email Services are a Salesforce feature where an email address is linked to an Apex class. When mail arrives at that address, Salesforce calls your class and passes it the message. Your class returns a result saying whether it succeeded.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a post box with a clerk behind it. The clerk opens each letter, reads it and files it in the right cabinet. Your Apex class is that clerk.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Inbound Email Services Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Many businesses still receive work by email. Quotes, enquiries, supplier confirmations and forms all arrive as messages that someone copies into the system by hand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inbound Email Services remove that step. They also handle cases that Email-to-Case cannot, because you choose the object, the parsing rules and what happens when the sender is unknown.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Inbound Email Services Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce routes the mail to your class, which must implement one interface and one method.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-1-1024x640.png\" alt=\"Inbound Email Services diagram showing how an email becomes a Salesforce record\" class=\"wp-image-2386\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How an incoming email becomes a Salesforce record.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The Handler Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The class must be <code>global<\/code> and implement <code>Messaging.InboundEmailHandler<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global class EnquiryHandler implements Messaging.InboundEmailHandler {\n\n    global Messaging.InboundEmailResult handleInboundEmail(\n            Messaging.InboundEmail email,\n            Messaging.InboundEnvelope envelope) {\n\n        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();\n        try {\n            Lead l = new Lead();\n            l.LastName = (email.fromName == null) ? 'Unknown' : email.fromName;\n            l.Company  = 'Email enquiry';\n            l.Email    = envelope.fromAddress;\n            l.Description = email.plainTextBody;\n            insert l;\n            result.success = true;\n        } catch (Exception e) {\n            result.success = false;\n            result.message = 'Failed: ' + e.getMessage();\n        }\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Setting <code>success<\/code> to false tells Salesforce to bounce the message back to the sender with your text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What the Email Object Gives You<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This table lists the fields you will read most often.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Property<\/th><th>Holds<\/th><\/tr><\/thead><tbody><tr><td>subject<\/td><td>The subject line<\/td><\/tr><tr><td>plainTextBody<\/td><td>The message as plain text<\/td><\/tr><tr><td>htmlBody<\/td><td>The message as HTML<\/td><\/tr><tr><td>fromName<\/td><td>The sender&#8217;s display name<\/td><\/tr><tr><td>toAddresses<\/td><td>Who it was sent to<\/td><\/tr><tr><td>ccAddresses<\/td><td>Copied recipients<\/td><\/tr><tr><td>binaryAttachments<\/td><td>Files such as PDF or images<\/td><\/tr><tr><td>textAttachments<\/td><td>Plain text attachments<\/td><\/tr><tr><td>messageId<\/td><td>The unique message identifier<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The envelope is separate and holds <code>fromAddress<\/code> and <code>toAddress<\/code>, which are the real routing addresses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Attachments<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if (email.binaryAttachments != null) {\n    List&lt;ContentVersion&gt; files = new List&lt;ContentVersion&gt;();\n    for (Messaging.InboundEmail.BinaryAttachment a : email.binaryAttachments) {\n        ContentVersion cv = new ContentVersion();\n        cv.Title = a.fileName;\n        cv.PathOnClient = a.fileName;\n        cv.VersionData = a.body;\n        files.add(cv);\n    }\n    insert files;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Saving as <code>ContentVersion<\/code> creates a modern Salesforce File rather than an old attachment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parsing the Body<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Real emails need rules. A simple approach reads labelled lines.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static Map&lt;String, String&gt; parse(String body) {\n    Map&lt;String, String&gt; out = new Map&lt;String, String&gt;();\n    if (String.isBlank(body)) { return out; }\n    for (String line : body.split('\\n')) {\n        if (line.contains(':')) {\n            List&lt;String&gt; parts = line.split(':', 2);\n            out.put(parts[0].trim().toLowerCase(), parts[1].trim());\n        }\n    }\n    return out;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An email with <code>Student: Asha Mehta<\/code> then gives you a map you can read by key.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up the Email Service<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After the class is saved, an admin creates the service in Setup.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open Setup and type &#8220;Email Services&#8221; in Quick Find.<\/li>\n\n<li>Click New Email Service and give it a name.<\/li>\n\n<li>Choose your Apex class in the Apex Class field.<\/li>\n\n<li>Set Accept Attachments to All if you need files.<\/li>\n\n<li>Save, then click New Email Address to generate the receiving address.<\/li>\n\n<li>Copy the long generated address and test by emailing it.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">These are the settings that matter most.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>Why it matters<\/th><\/tr><\/thead><tbody><tr><td>Apex Class<\/td><td>The handler that runs<\/td><\/tr><tr><td>Accept Attachments<\/td><td>None, text, binary or all<\/td><\/tr><tr><td>Accept Email From<\/td><td>Restricts senders, blank allows all<\/td><\/tr><tr><td>Active<\/td><td>Must be ticked to receive mail<\/td><\/tr><tr><td>Failure Response Settings<\/td><td>What happens on errors and limits<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Inbound Email Services vs Email-to-Case<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both turn email into records. This table shows the difference.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Inbound Email Services<\/th><th>Email-to-Case<\/th><\/tr><\/thead><tbody><tr><td>Target object<\/td><td>Any<\/td><td>Case only<\/td><\/tr><tr><td>Custom parsing<\/td><td>Full control<\/td><td>Limited<\/td><\/tr><tr><td>Setup effort<\/td><td>Apex plus config<\/td><td>Config only<\/td><\/tr><tr><td>Threading replies<\/td><td>You build it<\/td><td>Built in<\/td><\/tr><tr><td>Who maintains it<\/td><td>Developer<\/td><td>Admin<\/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\/apex-inbound-email-services-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-2-1024x640.png\" alt=\"Inbound Email Services compared with Email-to-Case in Salesforce\" class=\"wp-image-2387\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Inbound Email Services compared with Email-to-Case.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use Email-to-Case for a normal support inbox. Use Inbound Email Services when you need custom parsing or a different object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Inbound Email Services Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path receives enquiries at one address. Each email names a course and a student, and the team wants a Lead created and linked to that course.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Handler<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>global class CourseEnquiryHandler implements Messaging.InboundEmailHandler {\n    global Messaging.InboundEmailResult handleInboundEmail(\n            Messaging.InboundEmail email,\n            Messaging.InboundEnvelope env) {\n\n        Messaging.InboundEmailResult res = new Messaging.InboundEmailResult();\n        Map&lt;String, String&gt; data = EmailParser.parse(email.plainTextBody);\n\n        String courseName = data.get('course');\n        List&lt;Course__c&gt; found = [SELECT Id FROM Course__c\n                                 WHERE Name = :courseName LIMIT 1];\n\n        Lead l = new Lead(\n            LastName = data.containsKey('student') ? data.get('student') : 'Email enquiry',\n            Company  = 'Web enquiry',\n            Email    = env.fromAddress,\n            LeadSource = 'Email',\n            Description = email.subject + '\\n' + email.plainTextBody\n        );\n        if (!found.isEmpty()) { l.Course__c = found[0].Id; }\n\n        insert l;\n        res.success = true;\n        return res;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Testing It<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You never send real mail in a test. Instead you build the objects and call the method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class CourseEnquiryHandlerTest {\n    @isTest static void createsLead() {\n        Messaging.InboundEmail email = new Messaging.InboundEmail();\n        email.subject = 'Enquiry';\n        email.plainTextBody = 'Student: Asha Mehta\\nCourse: Python Basics';\n        email.fromName = 'Asha Mehta';\n\n        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();\n        env.fromAddress = 'asha@example.com';\n\n        Test.startTest();\n        new CourseEnquiryHandler().handleInboundEmail(email, env);\n        Test.stopTest();\n\n        Lead l = [SELECT LastName, Email FROM Lead LIMIT 1];\n        System.assertEquals('Asha Mehta', l.LastName);\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 limits apply to Inbound Email Services.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Limit<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>Daily messages processed<\/td><td>1,000 times your user licence count, per org<\/td><\/tr><tr><td>Message size including attachments<\/td><td>25 MB<\/td><\/tr><tr><td>Attachment portion<\/td><td>Around 10 MB<\/td><\/tr><tr><td>Over-limit behaviour<\/td><td>Bounce, discard or requeue, as configured<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Inbound Email Services<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting the global keyword, so the class cannot be chosen in Setup.<\/li>\n\n<li>Assuming plainTextBody is filled, when some senders provide HTML only.<\/li>\n\n<li>Trusting the sender address, which is easy to forge.<\/li>\n\n<li>Running SOQL per email line instead of one query for the whole message.<\/li>\n\n<li>Not setting Accept Email From, which leaves the address open to spam.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Guarding Against Spam<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because the address is public once shared, restrict it. Set Accept Email From to your known domains, and check the sender exists in Salesforce before creating records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Inbound Email Services<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Wrap the whole method in try and catch, then set a clear failure message.<\/li>\n\n<li>Fall back to htmlBody when plainTextBody is blank.<\/li>\n\n<li>Store the original message on the record for an audit trail.<\/li>\n\n<li>Keep the handler thin and put parsing in a separate class you can test.<\/li>\n\n<li>Restrict senders and monitor the daily limit.<\/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 handler that creates a Task from any email, using the subject as the Task subject. Hint: your class must be global and implement Messaging.InboundEmailHandler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Inbound Email Services Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Inbound Email Services questions appear in integration interviews.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Inbound Email Services Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What are Inbound Email Services in Salesforce?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inbound Email Services connect an email address to an Apex class, so arriving mail runs your code and can create records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Which interface must the class implement?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Messaging.InboundEmailHandler, with one method called handleInboundEmail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: Why must the class be global?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the platform calls it from outside your code, so it needs global access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What two parameters does the method receive?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An InboundEmail with the content and an InboundEnvelope with the routing addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: What does the method return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Messaging.InboundEmailResult, whose success flag tells Salesforce whether to accept the mail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Where do you read the sender&#8217;s real address?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From the envelope, using fromAddress, rather than from the email body.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: How do you get the attachments?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Through binaryAttachments and textAttachments on the email object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Where is the service configured?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Setup under Email Services, where you pick the class and generate an address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Inbound Email Services Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: What happens when success is false?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce bounces the message back to the sender with your message text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How do you test an email handler?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Build InboundEmail and InboundEnvelope objects in the test and call the method directly, since no real mail is sent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: What is the daily processing limit?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is 1,000 messages for each user licence in the org, counted across all email services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: What is the maximum message size?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">About 25 MB including attachments, with roughly 10 MB available for the attachment part.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you store an attachment as a modern file?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ContentVersion with VersionData set to the attachment body, then link it to the record.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Why might plainTextBody be empty?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the sender&#8217;s client sent HTML only. So fall back to htmlBody and strip the tags.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: How do you stop unknown senders?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set Accept Email From on the service, and check the address against your records inside the handler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Can one class serve several addresses?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Several email services can point at the same class, and the envelope tells you which address received it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Inbound Email Services Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: emails arrive in bursts and some are lost. What do you check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The daily limit, the over-limit action and whether the handler throws. Set the failure response to requeue rather than discard.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: How do you make processing idempotent?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Store the messageId on the record and skip any message you have already handled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: Why keep parsing out of the handler?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because parsing is where the logic and the bugs live. A separate class is easier to unit test without building email objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Scenario: a supplier changes their email format and records break. How do you protect yourself?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Validate the parsed values, fail clearly with a bounce message and log the raw body so you can adjust the rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do governor limits apply here?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The handler runs in a normal transaction, so the usual SOQL and DML limits apply per message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Can you call a web service from the handler?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but keep it short. Better to queue the work with a Queueable so the email transaction finishes quickly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: How do you secure a public email address?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Restrict senders, verify against known contacts, and never execute instructions found in the body.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: When would you choose Email-to-Case instead?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the goal is a normal support inbox with threading and no custom parsing, since it needs no code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Do Inbound Email Services work in a sandbox?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but each sandbox generates its own address. So remember to update any sender or test script after a refresh.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Can Inbound Email Services update an existing record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Look up the record by a key in the subject or body, then update it instead of inserting a new one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: How do you handle replies to an existing thread?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Put a reference code in the subject when you send, then read that code on the way back. Otherwise replies create duplicates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: What happens if the handler throws an unhandled error?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The transaction rolls back and the platform applies your failure response setting. So catching errors yourself gives a far better message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: Are Inbound Email Services suitable for high volume?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Up to the daily cap they work well. However, for very large feeds an API or middleware integration is more reliable than email.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q30: Can an email service run as a specific user?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It runs in a system context rather than as the sender. So add your own sharing and access checks where they matter.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_classes_email_inbound.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Inbound Email<\/a><\/li>\n\n<li><a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=sf.code_email_services.htm&amp;type=5\" target=\"_blank\" rel=\"noopener noreferrer\">Salesforce Help: Email Services<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_classes_email_inboundemail.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: InboundEmail Class<\/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>Inbound Email Services run Apex when mail arrives at an address.<\/li>\n\n<li>The class is global and implements Messaging.InboundEmailHandler.<\/li>\n\n<li>The envelope holds the real sender address.<\/li>\n\n<li>Tests build the email objects by hand, since no mail is sent.<\/li>\n\n<li>Restrict senders and watch the daily processing limit.<\/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 control approvals from code with <a href=\"https:\/\/swapnilzambare.com\/blog\/approval-processes-in-apex\/\">Approval Processes in Apex<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inbound Email Services let Salesforce receive an email and run your Apex code on it. A parent emails an enquiry address, and your class&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2385,"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":[246,8,96],"tags":[68,98,31],"swapz_series":[],"class_list":["post-2388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration-apis","category-salesforce","category-salesforce-developer","tag-advanced","tag-apex","tag-email"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Inbound Email Services in Salesforce Explained<\/title>\n<meta name=\"description\" content=\"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top 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\/apex-inbound-email-services\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inbound Email Services in Salesforce Explained\" \/>\n<meta property=\"og:description\" content=\"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-19T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:00:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-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=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Inbound Email Services in Salesforce with Apex Explained\",\"datePublished\":\"2026-10-19T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/\"},\"wordCount\":1724,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inbound-email-services-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Email\"],\"articleSection\":[\"Integration and APIs\",\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/\",\"name\":\"Inbound Email Services in Salesforce Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inbound-email-services-featured.png\",\"datePublished\":\"2026-10-19T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:09+00:00\",\"description\":\"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inbound-email-services-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inbound-email-services-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Inbound Email Services featured image: code editor card with inbound handler\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inbound-email-services\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inbound Email Services in Salesforce with Apex Explained\"}]},{\"@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":"Inbound Email Services in Salesforce Explained","description":"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top 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\/apex-inbound-email-services\/","og_locale":"en_US","og_type":"article","og_title":"Inbound Email Services in Salesforce Explained","og_description":"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-19T03:30:00+00:00","article_modified_time":"2026-11-15T06:00:09+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Inbound Email Services in Salesforce with Apex Explained","datePublished":"2026-10-19T03:30:00+00:00","dateModified":"2026-11-15T06:00:09+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/"},"wordCount":1724,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-featured.png","keywords":["Advanced","Apex","Email"],"articleSection":["Integration and APIs","Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/","name":"Inbound Email Services in Salesforce Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-featured.png","datePublished":"2026-10-19T03:30:00+00:00","dateModified":"2026-11-15T06:00:09+00:00","description":"Learn Inbound Email Services in Salesforce: InboundEmailHandler, parsing attachments, creating records, limits and testing, plus top interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inbound-email-services-featured.png","width":2400,"height":1260,"caption":"Inbound Email Services featured image: code editor card with inbound handler"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inbound-email-services\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Inbound Email Services in Salesforce with Apex Explained"}]},{"@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\/2388","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=2388"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2388\/revisions"}],"predecessor-version":[{"id":3203,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2388\/revisions\/3203"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2385"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2388"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}