User Story
Create a Apex Program which sends mass email to the contacts and leads.
Expected Outcome
Email sent successfully
or
The following errors occurred: ....
Code
public class SendEmail {
public static void SendEmailtoContacts(){
List<String> emailList = new List<String>();
List<Contact> conList = [SELECT email FROM Contact];
for(Contact con : conList){
emailList.add(con.email);
}
List<Lead> leadList = [SELECT email FROM Lead WHERE Company ='Farmers Coop. of Florida'];
for(Lead l : leadList){
emailList.add(l.email);
}
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setToAddresses(emailList);
message.setSubject('New Year Message');
message.setPlainTextBody('Happy new Year!');
Messaging.sendEmail(new List<Messaging.Email> {message});
}
}
Execute the Code to Send and Email
SendEmail.SendEmailtoContacts();