Messaging.reserveSingleEmailCapacity will thrown an error: System.NoAccessException: The organization is not permitted to send email if the Email Deliverability is not set in Salesforce. So, using it we can check whether the Email Deliverability is enabled or disabled using Salesforce Apex.
Sample Code:
Boolean emailDeliverabilityEnabled = true;
try {
Messaging.reserveSingleEmailCapacity( 1 );
} catch ( System.NoAccessException e ) {
System.debug( 'Error is ' + e.getMessage() );
emailDeliverabilityEnabled = false;
}
List < Messaging.SingleEmailMessage > listMessages = new List < Messaging.SingleEmailMessage >();
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
List < String > listToAddresses = new List < String > { 'Your Email Address' };
message.setToAddresses( listToAddresses );
message.setSubject( 'Testing' );
message.setUseSignature( false );
message.setHtmlBody(
'<b>Test Content</b>'
);
listMessages.add( message );
if ( listMessages.size() > 0 && emailDeliverabilityEnabled ) {
Messaging.sendEmail( listMessages );
}