+ Start a Discussion
Saikiran KolliSaikiran Kolli 

Sending Email to Mass User

Hi Every one,
I'm unable to send email to all my users. This below code is allowing me to send email around 20 users only, if the limit exceeds 20 its throwing an error message . I want to send atleast 100 users, Please help me.

I'm having student object, in that 4 categories are there . Based on selection of category email must be sent. Each category having arond 100 users.

SendEmail failed. First exception on row 0; first error: LIMIT_EXCEEDED, Too many bccAddresses addresses.: [bccAddresses, 66]
Error is in expression '{!send}' in component <apex:commandButton> in page sendemailtocategory: Class.CategoryEmail.send: line 77, column 1
VF page:

<apex:page standardController="Student__c" extensions="CategoryEmail" id="pg">
<apex:sectionHeader title="Send an E-mail"/>

    <script>
function read(){
var sel=document.getElementById("{!$Component.pg.fm.pb.pbs.pbsi.selectcat}").value;
//alert(sel);
var subj=document.getElementById("{!$Component.pg.fm.pb.pbs1.pbsi1.sub}").value;

var body=document.getElementById("{!$Component.pg.fm.pb.pbs2.pbsi2.bdy}").value;

if(sel!='' && subj !='' && body!='')
   readsel(sel,subj,body);
  else
  {
  alert('Please fill all values.');
  return false;
  }
}
</script>

<apex:form id="fm">
<apex:pagemessages />
<apex:actionFunction action="{!readsel}" name="readsel">
<apex:param value="sel" assignTo="{!selectcat}"/>
<apex:param value="subj" assignTo="{!subject}"/>
<apex:param value="body" assignTo="{!body}"/>
</apex:actionFunction>
<apex:pageBlock title="Sending E-mail" id="pb">
<apex:pageBlockSection id="pbs" columns="1">
<apex:pageblockSectionItem id="pbsi">
<apex:outputLabel id="ol" value="Category" for="Cat"/>
<apex:actionRegion >
<apex:SelectList value="{!selectcat}" size="1" id="selectcat"  >
    <apex:selectOption itemValue=""  itemLabel="-- Please select a category--"></apex:selectOption> 
      <apex:selectOptions value="{!Name}">
      </apex:selectOptions>
      <apex:actionSupport event="onchange" />
    </apex:SelectList>
     </apex:actionRegion>
</apex:pageblockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection id="pbs1" columns="1">
<apex:pageblockSectionItem id="pbsi1">
<apex:outputLabel value="Subject" for="sub"/>
<apex:inputText maxlength="100" value="{!subject}" id="sub"  size="75" />
</apex:pageblockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection id="pbs2" columns="1">
<apex:pageblockSectionItem id="pbsi2">
<apex:outputLabel value="Body" for="bdy"/>
<apex:inputTextarea value="{!body}" id="bdy" rows="20"  cols="89" richText="true"/>
</apex:pageblockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!send}" value="Send"  onclick="return read();" id="cmdbutton"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>


Controller: 

public with sharing class CategoryEmail {
    public String subject{get; set;} 
    public String body{get; set;} 
    public String selectcat {get; set;} 
    public List < Student__c > stud {get; set;} 
    public String selectedLocation {get;set;}
   
    public PageReference readsel() {
        system.debug('selectcategory111' + selectcat);
        return null;
    }
    public CategoryEmail(ApexPages.StandardController controller) {

    }
  public List < SelectOption > getName() {
        List < SelectOption > options = new List < SelectOption > ();
        options.add(new SelectOption('Enrichment', 'Enrichment'));
        options.add(new SelectOption('After School', 'After School'));
        options.add(new SelectOption('Pre – K', 'Pre – K'));
        options.add(new SelectOption('Summer Camp', 'Summer Camp'));
        return options;
    }

 public List<SelectOption>  getLocations() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select a Location--','--Select a Location--'));
        Id lmId = UserInfo.getProfileId();
        if(lmId == [SELECT Id FROM Profile Where Name = 'Location Manager'].Id)
        {
        for (Location__c l: [SELECT Name, isLocationManager__c FROM Location__c WHERE isLocationManager__c = 'yes'])
        options.add(new SelectOption(l.Name,l.Name));
        }
        else
        {
        for (Location__c l: [SELECT Name, isLocationManager__c FROM Location__c])
        options.add(new SelectOption(l.Name,l.Name));
        }
        return options ;
        
    }
    public PageReference send() {
         List < String > emailTo = new List < String >();
         List < String > emailCc = new List < String >();
         List< document > documentList=[select name from document where Name='logo'];
   
         emailTo.clear();
         emailCc.clear();
       
        stud = [select id, Name, Parent_Alternative_Email_ID__c, Parent_Email__c, Category__c from Student__c where Category__c = : selectcat];
 
        for (student__c std: stud) {
            if(std.Parent_Email__c !=null)
                 emailTo.add(std.Parent_Email__c);
            if(std.Parent_Alternative_Email_ID__c !=null)
                emailCc.add(std.Parent_Alternative_Email_ID__c);
        } 
        
        
       List<Messaging.SingleEmailMessage> mails= new List<Messaging.SingleEmailMessage>();
       Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        semail.setBccAddresses(emailTo);    
        semail.setCcAddresses(emailCc);
        semail.setSubject(subject);
     
        semail.setHtmlBody(body);
        mails.add(semail);
        if(!mails.isEmpty()){
        if(!semail.BccAddresses.isEmpty()){
         Messaging.sendEmail(mails);
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email has been sent');
                    ApexPages.addMessage(myMsg);
                    
        }
        else{
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING, 'No Students are availble for selecting Category');
                    ApexPages.addMessage(myMsg);
        
           }
        }
        mails.clear();
        subject='';
        body='';
        selectcat='';
        return null;
        
    }
    
}

 
Best Answer chosen by Saikiran Kolli
Ankit SehgalAnkit Sehgal
in line 129: change semail.setBccAddresses(emailTo) to semail.setToAddresses(emailTo); 

You can use Maximum 25 email address in ccand bcc address. this is salesforce limitation otherwise it will give exception.
refer below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

setToAddresses has a limit of 100 addresses

All Answers

Ankit SehgalAnkit Sehgal
in line 129: change semail.setBccAddresses(emailTo) to semail.setToAddresses(emailTo); 

You can use Maximum 25 email address in ccand bcc address. this is salesforce limitation otherwise it will give exception.
refer below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

setToAddresses has a limit of 100 addresses
This was selected as the best answer
Saikiran KolliSaikiran Kolli
Thank you so much Ankit, is there is any other way to increase limit in BccAddresses.