+ Start a Discussion
IvarIvar 

binaryAttachment problem

Hi everyone.

 

I am having some trouble with binary attachments in inbound emails and was hoping someone might have a solution.

 

I have an inbound email processor that accepts an email containing a .vcf file and a .jpeg file as attachments.  I create a lead and the idea is to attach the two incoming files to the lead as attachments.

 

The .vcf file works like a charm and appears on the lead record as expected, but the .jpeg doesn't. For some reason the email.binaryAttachments list is empty. I've also tried adding a bunch of other binary files to the inbound email and the sizes of the two attachment lists always stay at 1 and 0, respectively.

 

Does anyone have a clue?

 

 

		       	
        	//now add the attachments to the Lead
        	List<Attachment> attachments = new List<Attachment>();
        	
        	//add  binaries
        	try {
	        	for( Messaging.InboundEmail.BinaryAttachment bin: email.binaryAttachments){
	        		Attachment a = new Attachment();
	        		a.ParentId = l.Id;
	        		a.Name = bin.filename;
	        		a.Body = bin.body;
	        		attachments.add( a );
	        	}
        	}
        	catch( Exception e ){}
        	 
        	//add texts
        	try{
	        	for( Messaging.InboundEmail.TextAttachment txt: email.textAttachments){
	        		Attachment a = new Attachment();
	        		a.ParentId = l.Id;
	        		a.Name = txt.filename;
	        		a.Body = Blob.valueOf(txt.body);
	        		attachments.add( a );
	        	}
        	}
        	catch( Exception e ){}
        	 
        	try{
        		insert attachments;
        	}
        	catch( Exception e ){
        		throw e;
        	}
        	

 

 

Best Answer chosen by Admin (Salesforce Developers) 
IvarIvar

Aaaargh... this was my fault. I had the EmailService set to accept only Text Attachments.... silly man. Thanks anyway :)

All Answers

Ritesh AswaneyRitesh Aswaney

Your empty catch blocks are swallowing your exceptions (unless you've not pasted them on here for purposes of brevity).

 

Perhaps you should print the exceptions in your catch block to check if you're getting any.

IvarIvar

Yeah you're right I omitted the catch segments on purpose, I am sending a message to an outside logger in there. The exception is "Attempting to dereference a null object" which is what led me to check the sizes of the lists and got me to the conclusion I mentioned earlier, that the email.binaryAttachments list is empty for some strange reason.

IvarIvar

And just to add, the image I am sending is a 41K jpeg file so it should be well within any limits

IvarIvar

Aaaargh... this was my fault. I had the EmailService set to accept only Text Attachments.... silly man. Thanks anyway :)

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney

Nice one mate !