Based on the doc,
If you specify a contact for the targetObjectId
field, you can specify an optional whatId as well. This helps to
further ensure that merge fields in the template contain the correct
In your code, you set a User Id to targetObjectId field.
To resolve, verify the following permissions on the affected user's profile:
'Send Email' permission is enabled.
And try this too
if(u.Email != null && u.Email!='')
If you need any more help let me know ?
A single File (ContentDocument) can have many Versions (ContentDocumentVersion) which all link to the same record (ContentDocumentLink). Your code is pulling all the versions for the record which will include current and old versions. The easiest fix is to include isLatest to only pull the latest version of files.
List<ContentVersion> versions = [...
Florian's comment explains that the "Sandbox" prefix will disappear in Production.
For the senderDisplayName and mail id of the user showing, what's the value of what you're passing into setSenderDisplayname()? I copied and pasted your code and was able to see it display correctly. Be aware it's still using the user's email address from Salesforce to send ...