+ Start a Discussion
JT HargreavesJT Hargreaves 

Forwarding an incoming email in Salesforce with attachments and inline images.

I have an inbound mail handler setup in salesforce and I'm trying to forward the incoming email with attachments and embedded images to another address. Eventually, I need to create a new case and then include the case reference information but now I just need to prove that I can create the forward.
Any thoughts?
JT HargreavesJT Hargreaves
Here is the code I have and where it is failing:
public with sharing class ForwardingEmailProcessing implements Messaging.InboundEmailHandler {

    public Messaging.InboundEmailResult handleInboundEmail( Messaging.InboundEmail email, Messaging.InboundEnvelope envelope ) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
     
        //Send Email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     	String[] toAddresses = new String[] {'email@address.com'};
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('No Reply');
        mail.setSubject(email.Subject);
        mail.setPlainTextBody(email.plainTextBody);
       	mail.setHtmlBody(email.htmlBody);
     	
     	List<Id> attachmentIds = new List<Id>();
        if (email.binaryAttachments != null){
			for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments)
			{
				Attachment attachment = new Attachment();
                attachment.Name = bAttachment.fileName;
                attachment.Body = bAttachment.body;
                // FAILES HERE attachment.ParentId = email.messageId;
                insert attachment;
                attachmentIds.add(attachment.Id);
                
             }
        }     		
        mail.setEntityAttachments(attachmentIds);            
    
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
			

        return result;
    }

}