How do you use Batch for send more than 10 emails?

I have been watching how to implement the batch for sending more than 10 email but I get errors. When I send emails I have a limit 5 and also are sent several times to the same user email

global class SendBatchClass implements Database.Batchable<sObject>, Database.AllowsCallouts{
    String query = '';

    global Batchname(){
    }
    global SendBatchClass(String newBodyBatch,...){
       this.body = newBodyBatch;
       try{...}
       catch(){...}
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {

        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope) {
        SES sesEmail = new SES(as3.key,as3.secret);
        for(sObject s: scope){
            sesEmail.sendEmail(subRecipients,sender,subject,newBody);
        }
    }
    global void finish(Database.BatchableContext BC) {

    }
}

Answer

There is a governor limit that stops you calling Messaging.sendEmail(Messaging.Email[] emails, Boolean allOrNothing) more than 10 times. But each call can send many messages in one go.

So if your SES class is using that API internally, you will have to refactor that class to send all the emails at once and so it can be called for example like this:

global void execute(Database.BatchableContext BC, List<Contact> scope) {
    SES.Email[] emails = new SES.Email[]{};
    for(sObject s: scope){
        emails.add(new SES.Email(...));
    }
    SES sesEmail = new SES(as3.key,as3.secret);
    sesEmail.sendEmail(emails);
}

You should review the other email governor limits too.

Attribution
Source : Link , Question Author : aniespica , Answer Author : Keith C

Leave a Comment