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
SaravaSarava 

Apex - Attaching files to an Email

I am working on sending an email with the opportunity files based on certain criteria. I am able to send an email with the 'versionData' as an email body which is queried from 'Contentversion'. 
Problem: When i try to open the attachment from email it says 'Preview not available' and also i am not able to open the downloaded attachment too.
Please find the code below and let me know where I am going wrong. Thanks!
List<id> ContentDocumentids = new List<id>();

for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid=:'Object id'])
                {
                   ContentDocumentids.add(CDLink.ContentDocumentid);  
                }
                for ( ContentVersion cversion : [SELECT title, 
                                                        PathOnClient, 
                                                        versiondata 
                                                  FROM contentversion 
                                                  WHERE ContentDocumentId IN :ContentDocumentids  
                                                   AND title LIKE 'WOCF%'])
                 {
                  blob WOCFbody = cversion.versiondata;
                  system.debug('body : '+WOCFbody+'-----------'+cversion.title);
                  Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
                  efa.setFileName(war2.opportunity__r.name+'-'+cversion.title);
                  efa.setBody(WOCFbody);
                  fileAttachments.add(efa); 
                 }

 
Best Answer chosen by Sarava
Raj VakatiRaj Vakati
Here is the code. There is no file extension like Pdf or ms doc etc . 
 
List<id> ContentDocumentids = new List<id>();

for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid=:<b>'Object id'</b>])
			{
			   ContentDocumentids.add(CDLink.ContentDocumentid);  
			}
			for ( ContentVersion cversion : [SELECT title, 
													PathOnClient, FileType,
													versiondata 
											  FROM contentversion 
											  WHERE ContentDocumentId IN :ContentDocumentids  
											   AND title LIKE 'WOCF%'])
			 {
			  blob WOCFbody = cversion.versiondata;
			  system.debug('body : '+WOCFbody+'-----------'+cversion.title);
			  Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
			  efa.setFileName(war2.opportunity__r.name+'-'+cversion.title+'.'cversion.FileType);
			  efa.setBody(WOCFbody);
			  fileAttachments.add(efa); 
			 }

 

All Answers

Raj VakatiRaj Vakati
Here is the code. There is no file extension like Pdf or ms doc etc . 
 
List<id> ContentDocumentids = new List<id>();

for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid=:<b>'Object id'</b>])
			{
			   ContentDocumentids.add(CDLink.ContentDocumentid);  
			}
			for ( ContentVersion cversion : [SELECT title, 
													PathOnClient, FileType,
													versiondata 
											  FROM contentversion 
											  WHERE ContentDocumentId IN :ContentDocumentids  
											   AND title LIKE 'WOCF%'])
			 {
			  blob WOCFbody = cversion.versiondata;
			  system.debug('body : '+WOCFbody+'-----------'+cversion.title);
			  Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
			  efa.setFileName(war2.opportunity__r.name+'-'+cversion.title+'.'cversion.FileType);
			  efa.setBody(WOCFbody);
			  fileAttachments.add(efa); 
			 }

 
This was selected as the best answer
SaravaSarava
Cool. It worked. I have also tried passing the contentType for Emailfileattachment and that worked too. Thanks for the Answer.
Sagar BhatiaSagar Bhatia
What should we pass for a pdf type attachment . I am getting some error "INVALID_CONTENT_TYPE, An invalid value (pdf) was specified for contentType."
Zach Horton 16Zach Horton 16
Sagar, I just ran into this same problem today, what a coincidence!
ContentType for EmailFileAttachment uses the same ContentType values as Attachment. for pdf, use 'application/pdf'.
Here is a full list I found: https://salesforce.stackexchange.com/questions/102542/valid-values-for-contenttype
Sagar BhatiaSagar Bhatia
Thanks Zach , I was able to solve by adding '.pdf' to the FileName . But your link is very helpful . 
Alex Edelstein 9Alex Edelstein 9
fyi, I just built a Flow Action that uses this code (Thanks, Sarava!) https://unofficialsf.com/send-rich-email-with-the-new-sendhtmlemail-action/
Makam RuthvikMakam Ruthvik
Hi All,

Can any one explain me how to add this file to a classic email template. Where should we add it. What is the process.

Thanks in Advance