+ Start a Discussion

Upload a file to an external server using apex

Hi all,


I had come across this requirement below ...


I need to process the inbound email using the Email Services and upload the attachements if any in the email to an external server ..


I am able to configure the Email Service, create the apex class and process the subject and body text.

Now, I need to get the attachment from the email and upload it to an external server.


Is there any way to upload the attachment file using apex.


Thanks in Advance ...




Have far have you got?


Where do you want to store them - Amazon S3?


Have you implemented the Messaging interface part.


Storing attachments in SF looks like this


/// Stores attachments in the incoming email into SF System
	private static void storeAttachments (Messaging.InboundEmail email, String parentId)
		Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.binaryAttachments;
		Messaging.InboundEmail.TextAttachment[]   tAttachments = email.textAttachments;
		if (bAttachments != null)
			for (Messaging.InboundEmail.BinaryAttachment mailAttachment : bAttachments)
				String attachmentName   = mailAttachment.fileName;
				Blob   body             = mailAttachment.body;
				Attachment attachment   = new Attachment ();
				if(attachmentName == null || attachmentName == '' )
					attachmentName = 'noname';             
				attachment.name         = attachmentName;
				attachment.body         = body;
				attachment.parentId     = parentId;
				insert attachment;



I want to upload a file to one of our ftp server ...


I am able to get the attachment from the inbound email, but need to upload it a ftp server.


Been thinking about this and don't have solution which can be written in Apex/Javascript running completely inside Salesforce. 


You will need to build something externally which retrieves the files and handles the FTP part. Obviously you could invoke this from inside Salesforce easy enough.


I would personally look at using a tool like Talend, Informatic, CastIron these all can handle Web Service > FTP.


Hope this helps.