Mar 7, 2011

How to Send More than 10 E-mails

Hi All,

This is about how to send more than 10 E-mails. As we know we have a governor limit of sending 10 emails in a process so this is a workaround to send more than E-mails. We can do this by using batch class.

Lets say we have to send all System Administrators details of case logged by them today in a particular format. If suppose we have 15 System Administrators in our organisation, all of them logged 2-3 cases in a day and in the end of the day we want to send the details of case logged only by them in an Email. So here is the code :

global class Batch_CaseEmail implements Database.Batchable<sObject>,Database.Stateful
   Map<Id , List<Case>> userCaseMap {get; set;}
   List<Case> allCaseLoggedToday {get; set;}
   global Batch_CaseEmail()
       //Map to maintain user id and cases logged by them today
       userCaseMap = new Map<Id, List<Case>>() ;
       //All sales rep (System admins)
       List<User> salesRep = new List<User>() ;
       salesRep = [select id , name , Email , ManagerId from User where Profile.Name = 'System Administrator'] ;
       //All sales rep ids
       List<Id> salesIds = new List<Id>() ;
       for(User ur : salesRep)
           salesIds.add(ur.Id) ;
       //All cases logged today by sales rep
       allCaseLoggedToday = new List<Case>() ;
       allCaseLoggedToday = [select Id, CaseNumber,CreatedById, , account.Name , from Case where CreatedDate = TODAY AND CreatedById in : salesIds] ;

   global Database.QueryLocator start(Database.BatchableContext BC)
      //Creating map of user id with cases logged today by them
      for(Case c : allCaseLoggedToday)
              //Fetch the list of case and add the new case in it
              List<Case> tempList = userCaseMap.get(c.CreatedById) ;
              //Putting the refreshed case list in map
              userCaseMap.put(c.CreatedById , tempList) ;
              //Creating a list of case and outting it in map
              userCaseMap.put(c.CreatedById , new List<Case>{c}) ;

      //Batch on all system admins (sales rep)
      String query = 'select id , name , Email from User where Profile.Name = \'System Administrator\'';
      return Database.getQueryLocator(query);

   global void execute(Database.BatchableContext BC, List<sObject> scope)
      for(Sobject s : scope)
          //Type cast sObject in user object
          User ur = (User)s ;
          //If system admin has logged any case today then only mail will be sent
              //Fetching all cases logged by sys admin
              List<Case> allCasesOfSalesRep = userCaseMap.get(ur.Id) ;
              String body = '' ;
              //Creating tabular format for the case details
              body = BodyFormat(allCasesOfSalesRep) ;
              //Sending Mail
              Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
              //Setting user email in to address
              String[] toAddresses = new String[] {ur.Email} ;
              // Assign the addresses for the To and CC lists to the mail object
              mail.setToAddresses(toAddresses) ;
              //Email subject to be changed
              mail.setSubject('New Case Logged');
              //Body of email
              mail.setHtmlBody('Hi ' + ur.Name + ',<br/><br/> Details of Cases logged today is as follows : <br/><br/>' + body + '<br/> Thanks');
              //Sending the email
              Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   public String BodyFormat(List<Case> lst)
       String str = '' ;
       for(Case cs : lst)
           str += '<tr><td>'+ cs.CaseNumber +'</td>'+'<td>'+ cs.Owner.Name +'</td>'+'<td>'+ cs.Account.Name +'</td>'+'<td>'+ cs.Contact.Name +'</td>'+'</tr>' ;
       str = str.replace('null' , '') ;
       String finalStr = '' ;
       finalStr = '<table border="1"> <td> CaseNumber </td> <td> Owner </td> <td> Account </td> <td> Contact </td> '+ str +'</table>' ;
       return finalStr ;

   global void finish(Database.BatchableContext BC)


Now to execute this batch class we can schedule it or simply run this code in system logs for testing :

Batch_CaseEmail controller = new Batch_CaseEmail() ;
Integer batchSize = 1;
database.executebatch(controller , batchSize);

So when we execute it all system administrators will get E-mails in this format 



  1. Why not just send it to everyone at the same time e.g.

    String[] toAddresses = new String[] {'', '', ''}

    This would then count as 1 email.

    1. IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Online Training in India

      JavaScript Training in India

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. Body of each email is different and is according to the case logged in a day by each user.

  3. Now question is what is the limit for sending emails per day. Then here it is :

    Limit for unlimited edition is 1000 email per day, professional edition is 250 and enterprise edition is 500.
    But if you are sending emails to internal users there is no limit on it.

    Here is some information I received from Salesforce on the daily email limit -

    What COUNTS towards the limit
    * Mass Emails to Contacts
    * Mass Email to Leads
    * Emails send via the API to email addresses
    * Emails send via the API to contacts (Both single and mass emails)
    * Emails send via API to Leads (both Single & Mass Emails)

    What DOES NOT COUNT towards the limit
    * Mass emails to Users
    * Emails via API to User ID’s
    * Emails send from the “Send Email” button on contacts (single emails/email author)
    * Emails send from the “Send Email” button on Leads (single emails/email author)
    * Workflow emails
    * System generated emails

  4. Can I call the method which sends email multiple times to overcome this limitation?
    For eg: sendandAttach() is a method which sends email(content for each recipients is different). Now if I call this method multiple times will the limits still be effective?

  5. I am not techy at all but am in the situation where I have been handed salesforce by our IT department to manage spontaenous volunteers in emergencies and natural disasters! Strange but true... We expect if this happens we will have several thousand people registered as 'customers' and will want to communicate by email with them all, but we have a 500 email limit. Do you know of any plug-ins we can use to by-pass this?

    1. This comment has been removed by a blog administrator.

  6. Hey, Thanks for this nice code. I am very new to Salesforce. I want to use my templates. So please tell me how to fetch my templates and these templates are specific to the selection of radio buttons I have in a form.

  7. Hi Ankit,

    I am working batch class but i am facing lost of issues. I am running batch class on 2 to 2.5lk records (records by query) in actual system i have 6-7lk records

    1. The query i have written in start method is taking too much time from preparing state to processing state.
    2. Some time i am getting this - "All attempts to execute message failed, message was put on dead message queue" exception.

    I have went through all salesforce docs. They suggest me to optimize query. below is my query.


    String query = '';

    query = 'select AccountName,Id, AccountNumber, Type, Total_Balance__c from Account where Total_Balance__c !=0 and CreatedDate = LAST_N_DAYS:3' order by Id';

    System.debug('Enter into START');

    return Database.getQueryLocator(query);

    Hitesh N. Patel

  8. You wouldn't happen to have test code to go with this would you. Yep, I'm a newbie and it would be appreciated.

  9. Thank you! The code to execute specifying a batch size is what I was missing.

  10. Hi Ankit,

    I am facing issue with Mass Email for Leads.I have enterprise edition and trying to send Mass Email using 'Mass Email Leads' Option and limit is 500 emails but i am getting the error 'This mass email cannot be sent today as your daily limit has only 7 emails remaining. If you are going to send today go back and select 7 or fewer recipients. You may schedule this mailing for delivery at a future date.' .I don't understand the reason.
    I also tried to schedule for future date but getting error.

    Please help

    1. This comment has been removed by the author.

  11. Very nice blog for software and related information, its very nice article. images are very clear to understand. thanks for sharing such great article hope keep sharing such kind of article Bulk Mailer Software

  12. Great post and Lessons, buddy. I think GetResponse is the best email marketing platform to boost your sales. Flexible, simple and cost-effective for landing page, webinar, and email automation Start your FREE trial now-@

  13. Anyone can also use the 1st mass mailer software for directkly send maximum email in one click.

  14. This Article explain what's the process email Marketing and how it enhance sale. Thank you for sharing these wonderful tips. It’s like a collecting of lots of email account including personal information. hope you keep sharing such kind of information Bulk Mailer Software

  15. Such an amazing blog.
    mass mailer software csn help you send bulk emails.

  16. Avro keyboard free download for Windows Ubuntu,
    Linux and MacOS operating system with full offline installer supports both 32-bit and 64-bit operation system.
    Download Bang

    Free download full version of bijoy ekattor 71 for windows with free activation process and installation process.
    User friendly interface, easiest typing system, lots of typing automation tool,
    free online support makes it the most popular Bangla typing software of today.

    Bijoy Bayanno 2018 full latest version free download for Windows Xp/7/8/10.
    This is a full latest version of Bijoy 52 with full activation serial key you can download from here.bijoy bangla keyboard

    Free download bijoy keyboard full version with 100% working serial key for Windows Xp, Windows 7,
    Windows 8, Windows 10 supports both 32-bit and 64-bit windows version. bijoy bayanno free download is the best Bangla Typing software

    bijoy bayanno free download

    download pc software for free

  17. IPL Season 12 Schedule – Complete Details for Indian Premier League 2019 Dates, Time, Venues, Teams & Fixtures
    IPL 2019 Schedule

  18. How to put 600000 records into one single file and that file send through email

  19. Hey Nice Blog!!! Thank you for sharing information. Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!!!

    Bulk Email Service Providers in Lucknow
    Bulk Sms Provider in Lucknow
    Best SEO Company in Munshipuliya,Lucknow
    Best SEO Expert in Lucknow

  20. Best information about software.Thanks for sharing such great information. hope you keep sharing such kind of information Bulk Mailer Pro

  21. Thanks for sharing such a nice blog. Web email grabber help you to grab the genuine list of emails from websites email extractor

  22. Good content and nice blog. Thanks for sharing

    such great information. hope you keep sharing such

    kind of information Advance Bulk Mailer

  23. Such a great blog ! Thanks for sharing this informative with us ! Good Content!
    Bulk Email Software

  24. I perceived the article to be merely worthwhile. Continue posting this tremendous work
    Website Designer in Lucknow | Web design company

  25. This post is absolutely great because I also would like to start using various bulk email as well as SMS marketing techniques in order to grow the sales for the home based business. We currently serve in the local area only would like to get an affordable app to bulk Send sms from slack.

  26. Nice Post thanks for the information, good information & very helpful for others,Thanks for Fantasctic blog and its to much informatic which i never think ..Keep writing and grwoing your self

    rc transfer in delhi
    rc transfer in ghaziabad
    rc transfer in noida
    rc transfer in gurgaon
    rc transfer in faridabad
    rc transfer in bangalore
    rc transfer in greater noida
    rc transfer in mumbai
    rc transfer in ranchi
    rc transfer in chennai

  27. This article is so useful, thanks for sharing with us Advance Bulk Mailer.

  28. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian

    court marriage in delhi ncr
    court marriage in delhi
    court marriage in noida
    court marriage in ghaziabad
    court marriage in gurgaon
    court marriage in faridabad
    court marriage in greater noida
    name change online
    court marriage in chandigarh
    court marriage in bangalore

  29. what an amazing way to explain your ideas. I am impressed with your writing skill. I am really glad to read this one. n this article you really explain the topic very well. thank you so much for this one. keep sharing your ideas and thoughts. Know more about ICO Development connect with us.

  30. this is something that i have been searching for a long time! at last you have ended my painful research thank you for that digital marketing agency

  31. You can use 1st mass mailer software for directkly send maximum email in one click.

  32. Sometime few such blogs become very helpful while getting relevant and new information related to your targeted area. As I found this blog and appreciate the information delivered to my database.

    Php projects with source code
    Online examination system in php
    Student management system in php
    Php projects for students
    Free source code for academic
    Academic project download
    Academic project free download

  33. Seo company in Varanasi, India : Best SEO Companies in Varanasi, India: Hire Kashi Digital Agency, best SEO Agency in varanasi, india, who Can Boost Your SEO Ranking, guaranteed SEO Services; Free SEO Analysis.

    Best Website Designing company in Varanasi, India : Web Design Companies in varanasi We design amazing website designing, development and maintenance services running from start-ups to the huge players

    Wordpress Development Company Varanasi, India : Wordpress development Company In varanasi, india: Kashi Digital Agency is one of the Best wordpress developer companies in varanasi, india. Ranked among the Top website designing agencies in varanasi, india. wordpress website designing Company.

    E-commerce Website designing company varanasi, India : Ecommerce website designing company in Varanasi, India: Kashi Digital Agency is one of the Best Shopping Ecommerce website designing agency in Varanasi, India, which provides you the right services.

  34. Nice blog I like to read your blog please keep it up.Website Design and Development are two different processes that get combined so that someone who visits a website gets a good user experience. Design is important and implementing that design in a right manner is development. Arkss Technologies is a Website Design & Development Company In USA having separate teams of Developers and Designers who work closely to design and develop your dream website.

  35. Nice blog I like to read your blog please keep it up.Website Design and Development are two different processes that get combined so that someone who visits a website gets a good user experience. Design is important and implementing that design in a right manner is development. Arkss Technologies is a Website Design & Development Company In USA having separate teams of Developers and Designers who work closely to design and develop your dream website.

  36. Hey!
    What an awesome post! This is so chock full of useful information I can’t wait to dig deep and start utilizing the resources u have given me. Your exuberance is refreshing.
    Thank you so much
    Other Services:
    Software Development And Advertising Agency
    Web Development Company In Lucknow
    Web Development Company In Lucknow
    Software Development Company In Lucknow
    Mobile App Development Company In Lucknow

  37. If you're looking any type of advertising services whether it's online or offline, Sigma Trade Wings is the right option for you.
    𝐅𝐨𝐫 𝐦𝐨𝐫𝐞 𝐝𝐞𝐭𝐚𝐢𝐥𝐬 𝐜𝐨𝐧𝐭𝐚𝐜𝐭 𝐮𝐬
    📱 7521888222