[SalesForce] Email Services Binary Attachment Not Working

I am using the Email Services in SFDC, and I am not able to get the attachments I attach to the emails to be uploaded to the new record that is being created. I built my APEX class off of the sample one in Force.com Cookbook. I have modified the code to work with my custom object, and that is working fine, the custom object record is being created just fine. The only thing I updated in the section of code that checks and attaches the attachments is the name for my custom object. Everything else is the same. Again, the custom object record is being created properly and consistently. It's just the attachments that are not showing up. I double checked to make sure the emails and attachments were under the file limit size, and they all are.

Here is my code:

// Creates new announcement object  

Announcements__c[] myAnnouncement = new Announcements__c[0];

global Messaging.InboundEmailResult handleInboundEmail(
    Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result =
        new Messaging.InboundEmailresult();

    // Records the senders email address

    String SendersEmail = envelope.fromAddress;

    // Retrieves and parses content from the email.

    // Splits each line by the terminating newline character and
    // looks for the position of announcement name, point of contact, and body copy 

    String[] emailBody = email.plainTextBody.split('\n', 0);
    String AnnouncementName = emailBody[0].substring(19);
    String PointOfContact = emailBody[2].substring(18);
    String AnnouncementBody = emailBody[4].substring(6);

    // Creates a new announcement from the information
    // retrieved from the inbound email  

    try {
        myAnnouncement.add(new Announcements__c(
            Senders_Email__c = SendersEmail,
            Name = AnnouncementName,
            Point_of_Contact__c = PointOfContact,
            Body__c = AnnouncementBody));

        insert myAnnouncement;
    } catch (System.DmlException e) {
        System.debug('ERROR: Not able to create announcement' + e);
    }

    // Searches the email for binary attachments and
    // associates them with the new announcement record

    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
        for (integer i = 0; i < email.binaryAttachments.size(); i++) {
            Attachment a = new Attachment(
                ParentId = myAnnouncement[0].Id,
                Name = email.binaryAttachments[i].filename,
                Body = email.binaryAttachments[i].body);
            insert a;
        } 
    }

    return result;
}

Any help would be appreciated.

Thank you.

Best Answer

This may sound stupid but what are your settings for attachments in the email service itself?

There doesn't seem to be an issue with the code.

enter image description here

Related Topic