+ Start a Discussion
Rakesh SRakesh S 

Send email every day 4 PM

Hi All,

i want to send email every day 4 pm. for that i written some code.
Please check it below code once and let me know, what i have to do.
 
global class scheduledEmail implements Schedulable {

    public static String CRON_EXP = '0 20 16 * * * *';

    global static String scheduleIt() {
        scheduledEmail sm = new scheduledEmail();
        return System.schedule('Monthly Email', CRON_EXP, sm);
    }
    
   global void execute(SchedulableContext SC) {
   
      sendmail();
      
   }
   public void sendmail()
       {
           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           string [] toaddress= New string[]{'sircilla.rakesh7@gmail.com'};
           email.setSubject('Testing Apex Scheduler-Subject');
           email.setPlainTextBody('Testing Apex Scheduler-Body');
           email.setToAddresses(toaddress);
           Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
       }
}

Please explain step by step.
I appriciate your response.


Thank you.
Rakesh.S
Best Answer chosen by Rakesh S
Prabhat Kumar12Prabhat Kumar12
Hi Rakesh,

If you want send email everyday at 4 pm you need to schedule the class to run. You can schedule your class through User Interface as well as through excuteAnoumous window from develop er console.

If you want to schedule it from user interface then follow the link below.

http://salesforce.stackexchange.com/questions/3593/scheduling-an-apex-job-through-the-ui

Scheduling through developer console.

Use below code in your apex class.
 
global class scheduledEmail implements Schedulable {

   global void execute(SchedulableContext SC) {
   
      sendmail();
      
   }
   public void sendmail()
       {
           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           string [] toaddress= New string[]{'sircilla.rakesh7@gmail.com'};
           email.setSubject('Testing Apex Scheduler-Subject');
           email.setPlainTextBody('Testing Apex Scheduler-Body');
           email.setToAddresses(toaddress);
           Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
       }
}

Go to developer console > Excute Anoumous.


Excute the below code it runs every day 4pm.
 
System.schedule('scheduledEmail', '0 0 16 * * ?', new scheduledEmail());

Let me know if does not work for you.