Apex allows you send emails from code using the Messaging namespace. As usual Salesforce governor limits are at work and you could unwittingly hit your limits without needing to.
If you are sending email to users within your organisation, make sure you send the email to a User not an email address. Emails to Users are not included in this limit.
By way of example, consider a batch job that notifies the user submitting the job whether the job succeeded or failed:
Bad:
Avoid using an Email Address for a Salesforce user.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] { UserInfo.getUserEmail() }); mail.setSubject('Batch Job Status Report'); mail.setPlainTextBody('The job completed successfully. Have an A1 day!');
Better:
Use the Target Object Id to reference the Salesforce User.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(UserInfo.getUserId()); mail.setSaveAsActivity(false); mail.setSubject('Batch Job Status Report'); mail.setPlainTextBody('The job completed successfully. Have an A1 day!');
Note, you need to use: setSaveAsActivity(false) when using a target object Id as the default value is true.
Remember you can get a User Id from the CreatedBy, LastModifiedBy or OwnerId fields on an object. However, be aware that the OwnerId could instead be a Queue.
More Info: