+ Start a Discussion
siva@shiva.comsiva@shiva.com 

SINGLE_EMAIL_LIMIT_EXCEEDED, Failed to send email

how to handle SINGLE_EMAIL_LIMIT_EXCEEDED, Failed to send email

 

here is my code ...please check it

trigger createactivity on Task (before insert,after insert,before update,after update) {
if(trigger.isbefore)
{
    if (Trigger.isinsert) {
 
     for(task t:trigger.new){
      if(t.status=='completed'){
          sendmailto();    
       }
      }
    }

}

if(trigger.isafter){

if(trigger.isinsert)
{
 for(task t:trigger.new){
      if(t.status=='completed'){
       sendmailto();    
       }
      }
}
 if (Trigger.isupdate) {
 
     for(task t:trigger.old){
      if(t.status=='completed'){
       sendmailto();    
       }
      }
    }

}
public void sendmailto()
{
     Contact c=[select id,lastname,Account.Name,email  from contact limit 1];
     List<id> contactids;
     
     string []to=c.email.split(':',0);
     system.debug('************************************'+to);
     if(c.email<>null)
     {
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     
     email.setToAddresses(to);
      //email.setTargetObjectId(c.id);  
      email.setSubject('this will be the task subject');
      email.setplainTextbody('I spoke with '+c.lastname+ 'today from '+c.account.name +'about the latest proposal.');
      messaging.sendemailresult[]r=messaging.sendemail(new messaging.singleemailmessage[]{email});
      }
          
       else
        {
            Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
            String[]  toAddresses = new String[] {'sivasalesforce.com@gmail.com'};
            mail1.setToAddresses(toAddresses);
            mail1.setplainTextbody('Invalid mail id'+c.email);
            //Messaging.sendEmail(new Messaging.SingleEmailMessage [] {mail1});
            messaging.sendemailresult[]r=messaging.sendemail(new messaging.singleemailmessage[]{mail1});

       }
     
}
}

souvik9086souvik9086

In dev org Total number of sendEmail methods allowed is 10. 

For your case it might be exceeding.

 

Check the limits

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

vbsvbs
@Shiva - There are governor limits w.r.t SendEmail calls in Salesforce. You are allowed 10 calls per transaction. As you are looping through in the trigger and calling sendemail this will be called more than 10 times for a normal bulk trigger invocation. Bulkify the calls to send email in blocks of 100 emails or make asynchronous Apex calls and consolidate the sendEmail calls.
Puja_mfsiPuja_mfsi

Hi,

Tell me one thing why r u create this trigger on before Insert and after insert at same condition.

Suppose you insert a record then your code is call two times at first before insert and then after insert,It also increase the count of calling sendmail method.

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left