5

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) {

    }
}
1
  • Do you mean that the batch size is 5? If the same user gets multiple emails, you can implement Database.Stateful and keep a set of already sent to email addresses and check that before sending.
    – Keith C
    Commented Jul 31, 2014 at 18:28

2 Answers 2

2

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.

2
  • Possibly too late for this, but any idea on the number of emails we can add to a list for .sendEmail() to fire off successfully?
    – t56k
    Commented Jan 13, 2016 at 8:16
  • 1
    @CD-RUM Take a look at the "Outbound Email: Limits for Single and Mass Email Sent Using Apex" section of Execution Governors and Limits which mentions max 100 at once and 1,000 per day for SingleEmailMessage.
    – Keith C
    Commented Jan 13, 2016 at 10:51
2

You can create a list of the Standard Messaging.SingleEmailMessage. You can then send all of the emails at once:

Messaging.sendEmail(Messaging.Email[], Boolean);

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_messaging.htm#apex_System_Messaging_sendEmail

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .