+ Start a Discussion
Nagarjuna Reddy.PNagarjuna Reddy.P 

Email from batch apex

Hello Everyone,

I just need to send email from finish method 
ple see my example code

global class batchExample implements Database.Batchable<sObject> {
    global Database.QueryLocator start( Database.BatchableContext BC ) {
        String query = 'SELECT Id,Name,email FROM contact';
        return Database.getQueryLocator( query );
    global void execute( Database.BatchableContext BC, List< Contact> scope ) {
         for ( Contact c : scope ){
            if(c.mailingCountry = = 'US')
             c.Name = c.Name + 'from US';            
         update scope;
    global void finish( Database.BatchableContext BC ) {
      Messaging.SingleEmailMessage mail = new         Messaging.SingleEmailMessage();

Here I need to send email to contacts owner and email field of contacts  processed in execute()
So how do i get these email ids 
Do i need to write SOQL query to get email ids, any guidance is much appreciated.
Thank you all
VinayVinay (Salesforce Developers) 

Below is the snippet for the finish method.
Public void finish(database.batchableContext bc){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setSenderDisplayName('Batch Processing user');
      mail.setSubject('Batch Execution Completed');
      mail.setPlainTextBody('This email marks the completion of batch job');
      Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});