function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh SRakesh S 

Schedulable class for sending an email

Hi All,

Written a schedulable class for sending an email on every wednesday.

My schedulable class:
global class ClassEmailSchedule implements Schedulable {
    
    public List<Log_Book__c> lb{get;set;}
	global void execute(SchedulableContext SC){
        
        sendMail();
    }
    public void sendMail(){
        
        lb = [SELECT ID, NAME, Subject__c, Student__r.NAME,Class_Scheduled_Date__c, Student__r.Parent_Email__c, Subjects_Registered__r.Select_Day__c 
            FROM Log_Book__c WHERE Subjects_Registered__r.Select_Day__c = 'Wednesday' ];
        
        for(Log_Book__c email : lb){
            if(email.Class_Scheduled_Date__c == Date.today()){
		List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();

		Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
    
      List<String> sendTo = new List<String>();
      sendTo.add(email.Student__r.Parent_Email__c);
      mail.setToAddresses(sendTo);
      
      mail.setReplyTo('bhaskar.thamma@gmail.com');
      mail.setSenderDisplayName('BHASKAR');
    
       // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('jagadeesh.adaveni@gmail.com');
      mail.setCcAddresses(ccTo);

      mail.setSubject('Reminder for Class');
      String body = 'Dear <b>' + email.Student__r.name + '</b>, ';
      body += 'Your class is to day at 10.00 AM.';
      body += 'Thank you...';
      
      mail.setHtmlBody(body);
      mail.saveAsActivity = false;
      mail.setTargetObjectId(email.Id);

      mails.add(mail);
      // Step 5. Add your email to the master list
      Messaging.sendEmail(mails);
        }
    }
        
    }
}
Class scheduled but not getting any email.
Let me know what is the wrong in this and give me solution. 

Thank you...
Sandeep Mishra 7Sandeep Mishra 7
Hi Rakesh,

Code looks fine to me. Have you checked with Email Deliverability?
You can find it here: Setup->Administer->Email Administration->Deliverability.
Select Access Levels as 'All Emails'.
It should work now.

Regards,
Sandeep Mishra