single email limit exceeded salesforce very frequently..?

my email limit is exceeded after sending 10-12 emails..what could be the problem..as i have seen the limit is 1000 emails/day.i am not able to find out what could be the problem..Can some give me some suggestions. As i also know we can send 10 emails in a single transaction.I am sending emails in a batch class.

I am calling this batch class on button click and filling emailList with emailsId’s.

                    String query='Select id from Lead';//No use of this query
                    Batch_send_emails bse=new Batch_send_emails();
                    bse.query=query;                                    
                    bse.resendEmails=emailList;
                    database.executebatch(bse);


        **Batch Class**

         if(!resendEmails.isEmpty())
          {
              for(String s:resendEmails)
               {
                  emailsLeads.add(s);
               }
          }


        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(emailsLeads);
       // mail.setCcAddresses(ccAddresses);
        String subject='GoToMeeting Invitation Details for '+subject;
        String body='test'; 
        mail.setHtmlBody(body); 
        mail.setSubject(subject);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
        }  

Answer

There is a limit on the number of emails you can send from Apex, this is 5,000 per 24 hours and you would need to raise a case and engage with Salesforce to get this raised.

I think that the error message you are seeing there refers to a different limit, which is that you can only call sendEmail 10 times within the same context. Is it possible that your apex code is calling that in a loop?

You should do something like this

List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
//now do your loop
    .... {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(emailsLeads);
       // mail.setCcAddresses(ccAddresses);
        String subject='GoToMeeting Invitation Details for '+subject;
        String body='test'; 
        mail.setHtmlBody(body); 
        mail.setSubject(subject);
        allMails.add(mail);
 ....
 }
//Finished your loop? Now you have an array of mails to send.
//This uses 1 of your 10 calls to sendEmail that you are allowed
Messaging.sendEmail(allMails); 

sendEmail can be called on an array of messages, so rather than call it once you should build up an array of singleemailmessage and then send them all in one call.


Edit
A couple more things you can try to understand what the problem is

Before you make the sendEmail call, add

System.debug('You have made ' + Limits.getEmailInvocations() + ' email calls out of ' + Limits.getLimitEmailInvocations() + ' allowed');

When you look at the log you should see

You have made 1 email calls out of 10 allowed

If you see a series of messages (1,2,3 etc.) then you are calling it in a loop.

You can also check that there is capacity out of the 5,000 daily allowance still available (only if you are on a Winter ’14 instance).

try {
    Messaging.reserveSingleEmailCapacity(emailsLeads.size());
} catch (Exception e) {
    // From Winter '14, this code is now executed.
    System.debug('You have used up your daily allowance of mails');
}

Update – From Winter’17, limits have been raised to 5,000 instead of 1,000.

Attribution
Source : Link , Question Author : miku , Answer Author : Ysr Shk

Leave a Comment