[SalesForce] EmailMessage query returns ‘No such column’

I am trying to query emailMessage objects related to a custom object. When I try the query in the Developer console query editor it returns results, also works in workbench. However when I use the same query in an Apex class it gives an error.
The query is:

SELECT Id FROM EmailMessage WHERE RelatedToId = :customObject.Id 

And the error is:

No such column 'RelatedToId' on entity 'EmailMessage'.

The same happens with:

SELECT Id, RelatedToId FROM emailMessage

Also, probably related as is referencing the same relationship field, the following query isn't working:

SELECT (SELECT Id FROM Emails) FROM Custom_Object__c 

The background to this is trying to attach a file to the emailMessage, I attach in the apex class and it is sent with email however the emailMessage record doesn't have the attachment, is there an option to attach it, I can't find any, and this would solve my problem although I still can't understand why the above isn't working.

Best Answer

You need to enable the Enhanced Email feature in order to use that field in apex.

Also, if you want to relate the email message with a User, Lead or Contact, you need to use the EmailMessageRelation object.