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

Issues in Apex Email services

Hi All

I'm trying to use Apex Email services to add a record through Email services. However whenever I try to send email to the unique email id provided by platform, I get a delivery failure message


>>> (Undelivered): 554 System.StringException: Ending position out of

>>> bounds: 3  Class.EmailToApex.getFieldValue: line 5, column 16

>>> Class.EmailToApex.handleInboundEmail: line 11, column 44 External

>>> entry point


Did anybody faced similar issues while using Email Services?

Thanks in advance


you email service is working but some error in your code. Please paste your code.



global class EmailToApex implements Messaging.InboundEmailHandler {
    public static String getFieldValue(String plainTextBody, String pLabel) {
        Integer startPos = plainTextBody.indexOf(pLabel);
        Integer endPos = plainTextBody.indexOf('\n');
        return plainTextBody.substring(startPos+pLabel.length(), endPos);

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        String contactName =email.subject;
        Double mileageInt = Double.valueOf(getFieldValue(email.plainTextBody, 'Mileage:'));
        List<Contact> contactResult = new List<Contact>();
        contactName = '%'+contactName+'%';
        Mileage__c[] newMileage = new Mileage__c[0];
        try {
            for (Contact c :
                [Select Id, Name, Email From Contact Where Name like :contactName Limit 1]) {
                    newMileage.add(new Mileage__c(miles__c = mileageInt, Contact__c = c.Id));
            insert newMileage;
        } catch (System.Exception e) {
            System.debug('Error: ' + e);
        result.success = true; // If false, an email can be sent back with a message.
        return result;




your email.plainTextBody is null. You have to first check email body type and then pass value into your getFieldValue method.