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
kiran punurukiran punuru 

Unable to find the reason for the error

Hi ,
I am getting below error and unable to find the root cause please advise:
@@@Error Message:SendEmail failed. First exception on row 0; first error: INVALID_CONTENT_TYPE, An invalid value (ContentPost) was specified for contentType.: [fileAttachments, ContentPost] 

Thanks
kiran
Sagar PareekSagar Pareek
Hi Kiran,

Have you used setContentType method?

You need to set the MIME Content-Type of the email attachment using the setContentType method.

Thanks,
Sagar
kiran punurukiran punuru
Hi iam Using the same  PFB

public without sharing class SendEmail {
    public static void emailSenderMethod(Set<Id> feedItemIds){
       System.debug('@@@feedItemIds:'+feedItemIds); 
       List<FeedItem> lstFeedItem = [Select id,RelatedRecordId,type,body from FeedItem where Id IN:feedItemIds];
        for(Feeditem fd :lstFeedItem){
            if(fd.Type == 'ContentPost') {
                ContentVersion cv = [Select id,FileType, ContentDocumentId,ContentUrl,VersionData ,Title from ContentVersion Where Id = :fd.RelatedRecordId ];
                System.debug('@@@ Feed Body: ' + fd.Body);
                System.debug('@@@ Document Id: : ' + cv.ContentDocumentId);
                System.debug('@@@ Document Title: : ' + cv.Title);
               System.debug('@@@@ Document Type: '+fd.Type);
                Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                attach.setContentType(cv.FileType);
                attach.setFileName(cv.Title);
                attach.setInline(false);
                attach.Body = cv.VersionData;
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {'abc.xyz@test.com'};
                String[] ccAddresses = new String[] {'kirankumar.punuru@test.com'};
                mail.setUseSignature(false);
                mail.setToAddresses(toAddresses);
                mail.setCcAddresses(ccAddresses);
                mail.setReplyTo('kirankumar.punuru@test.com');
                mail.setSenderDisplayName('Kiran Kumar reddy Punuru');
                mail.setSubject('The Below File Has been uploaded : ');
                mail.setHtmlBody('Here is the email you requested: '+cv.Title);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                mail.setBccSender(false);
             try{
                   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
                }   
             catch(Exception e){
                    System.debug('@@@Error Message:'+e.getMessage());
                }  
             
             } 
          } 
    }
}
Sagar PareekSagar Pareek
In your case you are setting invalid content type - CONTENT POST thats why you are getting this error.
Please determine and provide the correct content type as mentioned in this post - http://salesforce.stackexchange.com/questions/2058/determine-the-correct-file-type-extension-for-an-attachment
kiran punurukiran punuru
Hi Sagar,

I am unable to get the point in that link .If you can understand please tell me
Thanks
Kiran 
VineetKumarVineetKumar
attach.setContentType(cv.FileType);
This will not set the correct content type for your attachment, cv.filetype will return file type ie., pdf, txt, png etc..
Content Type is something like : 
  • application/vnd.ms-excel - xls, xla, xlc, xlm, xlt, xlw
  • application/postscript - ai, eps, ps
  • application/vnd.ms-powerpoint - ppt, pot, pps
  • application/pdf - pdf
Swaraj Behera 7Swaraj Behera 7
Can you try this code
public without sharing class SendEmail {
  Map<string, string> mimeTypeMap = new Map<string, string>();
    public static void emailSenderMethod(Set<Id> feedItemIds){
       System.debug('@@@feedItemIds:'+feedItemIds); 
       List<FeedItem> lstFeedItem = [Select id,RelatedRecordId,type,body from FeedItem where Id IN:feedItemIds];
        for(Feeditem fd :lstFeedItem){
            if(fd.Type == 'ContentPost') {
                ContentVersion cv = [Select id,FileType, ContentDocumentId,ContentUrl,VersionData ,Title from ContentVersion Where Id = :fd.RelatedRecordId ];
                System.debug('@@@ Feed Body: ' + fd.Body);
                System.debug('@@@ Document Id: : ' + cv.ContentDocumentId);
                System.debug('@@@ Document Title: : ' + cv.Title);
               System.debug('@@@@ Document Type: '+fd.Type);
               
                mimeTypeMap.put('POWER_POINT_X', 'application/vnd.openxmlformats-officedocument.presentationml.presentation');
                mimeTypeMap.put('POWER_POINT', 'application/vnd.ms-powerpoint');
                mimeTypeMap.put('EXCEL', 'application/vnd.ms-excel');
                //You can add more

                Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                attach.setContentType(mimeTypeMap.get(cv.FileType));
                attach.setFileName(cv.Title);
                attach.setInline(false);
                attach.Body = cv.VersionData;
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {'abc.xyz@test.com'};
                String[] ccAddresses = new String[] {'kirankumar.punuru@test.com'};
                mail.setUseSignature(false);
                mail.setToAddresses(toAddresses);
                mail.setCcAddresses(ccAddresses);
                mail.setReplyTo('kirankumar.punuru@test.com');
                mail.setSenderDisplayName('Kiran Kumar reddy Punuru');
                mail.setSubject('The Below File Has been uploaded : ');
                mail.setHtmlBody('Here is the email you requested: '+cv.Title);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                mail.setBccSender(false);
             try{
                   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
                }   
             catch(Exception e){
                    System.debug('@@@Error Message:'+e.getMessage());
                }  
             
             } 
          } 
    }

 
VishnuReddyVishnuReddy
Filetype Field on ContentVersion object doesn't store MIME type of a file it stores extension . Use FileType Field on ContentDocument Object it will give you MIME type of file. Please mark this as best answer if it helps.
In line    attach.setContentType(cv.FileType); pass attach.setContentType(cv.contentDocument,FileType); this will work.