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
Nandhini S 3Nandhini S 3 

How to send mail to multiple recipients

Hi Guys,

I have a method which calls the below method to send out emails. I'm getting this error "Method does not exist or incorrect signature: void setTargetObjectId(List<String>) from the type Messaging.SingleEmailMessage".
Can someone help me out here.

private static Messaging.SingleEmailMessage createEmailMessageWithCc(List<String> toAddress, String template, Id whatId, List<String> targetObjectId,String ccAddress, Boolean saveAsActivity) {
       Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = toAddress;
        message.setTemplateId(template);
        message.setWhatId(whatId);
        message.setTargetObjectId ( targetObjectId);
        message.ccAddresses = new String[] { ccAddress };
        message.setsaveAsActivity(saveAsActivity);
       return message;
    }
SwethaSwetha (Salesforce Developers) 
Have you fetched the target objectids in your code?
You might want to try something like 
 
public class testemail
{
  private final List<Id> contactids;
  public List<Contact> con;
  public testemail(ApexPages.StandardController controller)
  {
     con = [select Id from Contact limit 250 ];
     for(Integer i=0;i<250;i++)
     {
         contactids.add(con[i].Id);
      }  
  }

  public void SendEmail()
  {
       Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
       mail.setTargetObjectIds(contactids);
       mail.setTemplateId('00X90000000QHUD');
       Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
  }  


}

Ref:https://www.forcetree.com/2009/07/sending-email-from-your-apex-class.html

Related: 
https://dfc-org-production.force.com/forums/ForumsMain?id=9062I000000DNKNQA4
https://salesforce.stackexchange.com/questions/190794/messaging-settargetobjectid-with-custom-objects

https://salesforce.stackexchange.com/questions/91771/method-does-not-exist-or-incorrect-signature-messaging-singleemailmessage-set

If this information helps, please mark the answer as best. Thank you
Nandhini S 3Nandhini S 3
Hi Swetha,

Yes, the method which is calling the createEmailMessageWithCc is passing a list<string> for setTargetObjectId .