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
Dastagiri BashaDastagiri Basha 

Hii All, I need to send a Mail to each and every contact recods. for that i have written Batch Class and I have scheduled a batch class. Mails are not getting delivered . Iam a begginer please help me with this.

Batch Class:::

global class BatchclasstoSendMail implements Database.Batchable <Sobject> {

     global Database.QueryLocator start(Database.BatchableContext bc){

     string query = 'select name,Email, Birthdate from Contact where Email != null';
     system.debug('******'+query);
     return Database.getQueryLocator(query);
     }

     global  void execute(Database.BatchableContext bc, List<Contact> lst){
  list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
          for(Contact c: lst){
            Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
            list<string> toadd=new list<string>();
            toadd.add(c.Email);
            system.debug('*****'+c.Email);
            objEmail.setToAddresses(toadd);
            system.debug('*****'+toadd);
            objEmail.setsubject('Your contact was registered successfully');
            objEmail.setplaintextbody('you are most valuable person we will contact you  as soon as possible');
            mail.add(objEmail); 
              system.debug('*****AllMails'+objEmail);
          }           
   Messaging.SendEmailResult[]  result =Messaging.sendEmail(mail);
         
      } 

     global void finish(Database.BatchableContext BC){
     System.debug(BC);
       
     }
   

Scheduled Class::

global class ScheduledBatchToSendMail implements schedulable {
global  void execute(SchedulableContext sc){
        BatchclasstoSendMail myBatchClass = new BatchclasstoSendMail();
            database.executeBatch(myBatchClass);
    }
}

Excecution scheduled class form DevConsole:::

ScheduledBatchToSendMail m = new ScheduledBatchToSendMail();
String sch = '0 30 18 ? * WED *';
String jobID = system.schedule('Merge Job', sch, m);
 
Best Answer chosen by Dastagiri Basha
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Dastagiri,

Can you please check if any of the email is invalid. Can you try sending email to only one contact and check if the batch is working fine. I tried using the same batch. As one of the email is invalid the entire batch got failed. 

Please check using one Id of the contact in the batch and run it in anonomous block.

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Dastagiri,

Can you please check if any of the email is invalid. Can you try sending email to only one contact and check if the batch is working fine. I tried using the same batch. As one of the email is invalid the entire batch got failed. 

Please check using one Id of the contact in the batch and run it in anonomous block.

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Dastagiri BashaDastagiri Basha
Thankyou Sai Pravee, Its working fine. Thankyou for your help