+ Start a Discussion
shiva1236shiva1236 

how to send an email to multiple Contacts

how to send an email template to multiple Contacts.
sunny522sunny522
Hi shiva1234,
We can use messaging.sendEmail() to send emails to contacts/accounts etc through apex.Please go through the links for reference.
http://salesforceglobe4u.blogspot.in/2017/06/how-to-send-email-through-apex-in.html
http://salesforceglobe4u.blogspot.in/2017/09/how-to-send-email-through-custom-button.html

 
Akhil TandonAkhil Tandon
You can send email to multiple contacts.
Use below mentioned code as a sample. 

http://developer.force.com/cookbook/recipe/creating-email-templates-and-automatically-sending-emails
 
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(User user :lstUser) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
string body = 'Hi '+ user.LastName;
mail.setSubject('Test Subject');
mail.setTargetObjectId(user.Id); mail.setSaveAsActivity(false);
mail.setHtmlBody(body); mails.add(mail);
}
Messaging.sendEmail(mails);
 
// In a separate class so that it can be used elsewhere
Global class emailHelper {

public static void sendEmail(ID recipient, ID candidate) {

  //New instance of a single email message
 Messaging.SingleEmailMessage mail = 
            new Messaging.SingleEmailMessage();
 
// Who you are sending the email to
   mail.setTargetObjectId(recipient);

   // The email template ID used for the email
   mail.setTemplateId('00X30000001GLJj');
          
   mail.setWhatId(candidate);    
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setReplyTo('recruiting@acme.com');
   mail.setSenderDisplayName('HR Recruiting');
   mail.setSaveAsActivity(false);  
 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }  
}