I have a code in which is basically used to send email about appointments But when I try to send for a bunch of people for a date interval. The number of email to be sent is increased and as a result the Messaging.sendEmail() is invoked more than ten times. Previously I used List mails = new List and added all the mails into this list as mails.add(mail) inside the for loop and sended as Messaging.SendEmail(mails). This was working some extent in Sandbox for a greater date range. But then when I deployed the code to production, error came as No_Mass_Mail_Permission Single email is not enabled for your organization.
public void mailblock(string email,string repid,string CCEmail,string alterEmails,string GroupMail,string Choice,string RepName){
// sendDocAttach(email,CCEmail);
system.debug('email: '+email);
system.debug('repid: '+repid);
system.debug('CCEmail: '+CCEmail);
system.debug('alterEmails: '+alterEmails);
system.debug('GroupMail: '+GroupMail);
system.debug('Choice: '+Choice);
system.debug('RepName: '+RepName);
System.debug('Mailblock function Entry:::::');
lstEvent=[Select Description,id,StartDateTime,LeadID__c,ActivityDate , EndDateTime,SalesrepID__c,Assigned_Sales_rep__c From Event where SalesrepID__c=:RepId and ActivityDate>=:Date.valueOf(ApexPages.currentPage().getParameters().get('fDate'))and ActivityDate<=:Date.valueOf(ApexPages.currentPage().getParameters().get('TDate')) ];
List<Date> seDate = new List<Date>();
DateTime FDate=DateTime.valueOf(ApexPages.currentPage().getParameters().get('fDate')+' 00:00:00');
DateTime TDate=DateTime.valueOf(ApexPages.currentPage().getParameters().get('tDate')+' 00:00:00');
if(FDate!=null && TDate!=null){
seDate = gensedate(FDate,TDate);
}
System.Debug('MyDate'+seDate);
Datetime myDT = Datetime.now();
List<Messaging.SingleEmailMessage>mails = new List<Messaging.SingleEmailMessage>();
if(seDate.size()>0){
for(Date d:seDate){
string sending='0';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { email });
mail.setCCAddresses(new String[] { CCEmail });
String[] BCCAddressList = new String[]{};
string emails='';
String[] BCCAddresses = new String[]{};
if(alterEmails!='' && alterEmails!=null){
BCCAddresses.add(alterEmails);
}
if(GroupMail!='' && GroupMail!=null){
BCCAddresses.add(GroupMail);
}
if(BCCAddresses.size()>0){
mail.setBCCAddresses( BCCAddresses );
}
// mail.setHtmlBody('http://premiercare.force.com/AppSheet?id='+RepId+'&fdate='+String.ValueOf(d).replace(' 00:00:00',''));
// Send the email
if(lstEvent.size()>0){
system.debug('lstEvent values:::::::');
for(Event E:lstEvent){
if(E.SalesrepID__c==RepId && E.ActivityDate==Date.valueOf(d)){
System.Debug('RepName++'+RepName);
string CS=string.ValueOf(d)+' 00:00:00';
AppEmailMap__c objAppEmail= new AppEmailMap__c();
objAppEmail.SalesRepId__c=RepId;
objAppEmail.SalesRep__c=RepId;
objAppEmail.AppDate__c=Date.valueOf(d);
insert objAppEmail;
objAppEmail.Appt_Sheet_Link__c=URL.getSalesforceBaseUrl().toExternalForm()+'/apex/VFAppoinment?id='+RepId+'&fdate=' +String.ValueOf(d).replace(' 00:00:00','')+'&EId='+string.valueof(objAppEmail.Id);
update objAppEmail;
mail.setSubject(RepName+' check your Appoinment(s) on ' + String.ValueOf(DateTime.ValueOf(CS).format('MM-dd-yyyy')).replace(' 00:00:00',''));
if(checkboxValue=='false')
{
Document doc = [SELECT Id,Name FROM Document WHERE Name = 'Required Finance Documents Form'];
mail.setDocumentAttachments(new ID[]{doc.id});
}
mail.setHtmlBody('Your AppointmentSheet:'+'To View Appointments <a href=http://premiercare.force.com/AppSheet?id='+RepId+'&fdate=' +String.ValueOf(d).replace(' 00:00:00','')+'&EId='+string.valueof(objAppEmail.Id)+'>click here</a><br/><br/><b><i><font size="2">Please keep in mind that appointment timesheet links are only accessible within 24 hours of the appointment date. Please check back if necessary (i.e. Holidays, etc.) as it is your responsibility to attend appointments on time regardless of when your timesheets are received.</font></i></b>');
//mails.add(mail);
system.debug('mails: '+mails);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
sending='1';
break;
}
}
//Messaging.sendEmail(mails);
system.debug('executed');
}
if(Choice=='1'){
if(sending=='0'){
System.Debug('RepName++&'+RepName);
string CS=string.ValueOf(d)+' 00:00:00';
if(checkboxvalue=='false')
{
Document doc = [SELECT Id,Name FROM Document WHERE Name = 'Required Finance Documents Form'];
mail.setDocumentAttachments(new ID[]{doc.id});
}
mail.setSubject(RepName+' Appoinment not Scheduled on ' + String.ValueOf(DateTime.ValueOf(CS).format('MM-dd-yyyy')).replace(' 00:00:00',''));
mail.setHtmlBody('Appointment not available on'+ String.ValueOf(DateTime.ValueOf(CS).format('MM-dd-yyyy')).replace(' 00:00:00','') );
// Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
mails.add(mail);
}
}else{
sending='1';
}
}
Messaging.sendEmail(mails);
}else{
system.debug('Else part Entry');
send(email,repid,CCEmail,alterEmails);
}
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email + ' and '+ CCEmail ));
}