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
Gaurav AgnihotriGaurav Agnihotri 

Send email to multiple users.

Hi, 
I have created a batch apex job and want to send emails to all the team members when the job is completed. Right now, I am only able to send one email. I intend to send emails to all the team members.  Below is the  class that I have created. 
global class batchPPBInsert implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //Query Record
    }
   
    global void execute(Database.BatchableContext BC, List<Pelco_Price_Book_Stage__c> scope)
    {
        //execute db operation
    }   
    global void finish(Database.BatchableContext BC)
    {
        String email;
        email='gagnihot@gmail.com';
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		mail.setToAddresses(new String[] {email});
		mail.setReplyTo('gagnihot@gmail.com');
		mail.setSenderDisplayName('Batch Processing');
		mail.setSubject('Batch Process Completed');
		mail.setPlainTextBody('Batch Process has completed');	
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Thanks, 
Gaurav
Best Answer chosen by Gaurav Agnihotri
Himanshu ParasharHimanshu Parashar

Hi Gaurav,

You can pass the list of email ids in setToAddresses
 
global class batchPPBInsert implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //Query Record
    }
   
    global void execute(Database.BatchableContext BC, List<Pelco_Price_Book_Stage__c> scope)
    {
        //execute db operation
    }   
    global void finish(Database.BatchableContext BC)
    {
        //String email;
        //email='gagnihot@gmail.com';
        String[] toAddresses = new String[]{'gagnihot@gmail.com','gagnihot@gmail1.com'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		mail.setToAddresses(toAddresses);
		mail.setReplyTo('gagnihot@gmail.com');
		mail.setSenderDisplayName('Batch Processing');
		mail.setSubject('Batch Process Completed');
		mail.setPlainTextBody('Batch Process has completed');	
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}


Let me know if that works for you.


Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar

Hi Gaurav,

You can pass the list of email ids in setToAddresses
 
global class batchPPBInsert implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //Query Record
    }
   
    global void execute(Database.BatchableContext BC, List<Pelco_Price_Book_Stage__c> scope)
    {
        //execute db operation
    }   
    global void finish(Database.BatchableContext BC)
    {
        //String email;
        //email='gagnihot@gmail.com';
        String[] toAddresses = new String[]{'gagnihot@gmail.com','gagnihot@gmail1.com'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		mail.setToAddresses(toAddresses);
		mail.setReplyTo('gagnihot@gmail.com');
		mail.setSenderDisplayName('Batch Processing');
		mail.setSubject('Batch Process Completed');
		mail.setPlainTextBody('Batch Process has completed');	
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}


Let me know if that works for you.


Thanks,
Himanshu
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
It works!!!

Thanks