NO_MASS_MAIL_PERMISSION Error from Unit Test on SingleMailMessage

I am currently working in a Config-Only sandbox. I have a unit test that is continually failing with the following error message:

System.EmailException: SendEmail failed. First exception on row 0;
first error: NO_MASS_MAIL_PERMISSION, Single email is not enabled for
your organization or profile. Single email must be enabled for you to
use this feature.: []

The interesting thing is that this unit test passes just fine in production. The unit test is for a batchable class. The error is occurring in the finish method on the Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); call. The finish method is:

global void finish(Database.BatchableContext bc) {
    AsyncApexJob a = [
        Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
        From AsyncApexJob
        Where Id = :bc.getJobId()
    ];

    // Send an email to the Apex job's submitter notifying of job completion.
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {a.CreatedBy.Email};
    mail.setToAddresses(toAddresses);
    mail.setSubject('Batch job complete with status: ' + a.Status);        
    String body = 'Batch job completed.';
    body += '\nThe batch job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.';
    body += '\nJob Results: ' + a.Id;
    mail.setPlainTextBody(body);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

I recently refreshed the sandbox from production and made no permission changes, so I have no idea why one org would be failing while the other is working just fine. The user I am attempting to run this class with is a System Administrator. The System Administrator profile has the Mass Email and Send Email permission selected. Has anyone run into an error like this before?

Answer

Have you checked the “Access to Send Email” option under Administration Setup – Email Administration – Deliverability? It’s new feature in Spring 13.

Attribution
Source : Link , Question Author : Jesse Altman , Answer Author : Kevin Lam

Leave a Comment