+ Start a Discussion
Victor EcheverríaVictor Echeverría 

Send email template through apex to various contacts

I want to send an email message using a template to all contacts related to an account when a certain event happens in the account. For this I have created a trigger. The issue is with my "mail.setTargetObjectIds(lstIds)" statement. I recieve the following error: Method does not exist or incorrect signature: void setTargetObjectIds(List<Id>) from the type Messaging.SingleEmailMessage.

The variable lstlds is a list of ids I got by quering all related contacts of the account.What could the error be? Can't I us a list on the setTargetObjectIds function?
Best Answer chosen by Victor Echeverría
NagendraNagendra (Salesforce Developers) 
Hi Victor,

Please find the sample code which will send email to various contacts using apex.
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
for(contact con : contactList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 
mail.setTargetObjectId(con>id)
mail.setSaveAsActivity(false);
allmsg.add(mail);
}
Messaging.sendEmail(allmsg,false);
Basically, add all contact in the list and then send them in a single shot. At one time you can send 100 emails.

I would also suggest you please check with below link which might help you further. Please let us know if this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra