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

send email to multiple users using visualforce email template



can u please tell me how to send email to multiple users by using visual force email template. if i send email to single user i am using setTargetObjectId(objContact.Id) and setWhatId(SR.Id). but i am not getting how to send to multiple users.


Please help me


Use this code


public void SendEmail(List<Id> userids)


Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();

List<Id> contactIds = new List<Id> ();

contactIds .add('003Q0000005DYvTIAW');

contactIds .add('003Q0000005DYvTIAW');

mail.setTargetObjectIds(contactIds); mail.setTemplateId('00XQ0000000HvBNMA0');


Messaging.SendEmailResult[] sendEmailResults = Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });


for (Messaging.SendEmailResult sendEmailResult : sendEmailResults) {

if(sendEmailResult.isSuccess()){ System.debug('success');


else{ System.debug(





Sam KanSam Kan



Thank u for sharing this code. Can u tell me,how to invoke this code in a button?  


Dear All,


 I have an email template which is given below:


Evaluation Request from {!Lead.LastName} {!Lead.FirstName} for {!Lead.SupportProduct__c}, is pending and requires manual approval. <br/>

Click <a href="{!ApprovalRequest.Internal_URL}">here</a> to approve or reject this request.<br/><br/>



Whenever I use this template through "Approval Processes" it works fine. But I have different situation in which I cannot use "Approval Processes". For example I need that when a Lead create then email should be generate for Lead Approval and send to those receptionist that are belongs to selected region. If a lead belongs to ASIA then email should be send on or if region is USA then it should be for I successfully completd this task with static or just text base template. But whenever I use Customise template its not filling merge values. Following is TRIGER code:


trigger TestEmail on Lead (after insert)
            MailerUtils.sendMail([0].Region__c, String.valueOf([0].Id),  [0].IsEmbargoe__c);



Now, following is MailerUtils class having sendMail method


public class MailerUtils
    public static void sendMail(string location, string leadId, Boolean IsEmbargoe)
        string message;
        string temp = 'ApproveLeadTemplate';
        String[] toAddresses;
        List<Id> idsList = getEmailAddresses(location);
        if(IsEmbargoe == False)
            temp = 'ApproveEmbargoeTemplate';
        EmailTemplate e = [select Id,Name,Subject,body from EmailTemplate where name like :temp+'%'];
        if(e != null || idsList==null)
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.saveAsActivity = false;

            // Send the email
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
            Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
            toAddresses = new String[] {''};
            message = 'This email will recieve by you only if Template Not Found!!!';
     public static List<Id> getEmailAddresses(string groupName)

        List<String> idList = new List<String>();
        List<Id> mailToIds = new List<Id>();
        Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = :groupName];
        for (GroupMember gm : g.groupMembers) {
        User[] usr = [SELECT Id, email FROM user WHERE id IN :idList];
        for(User u : usr) {
        return mailToIds;



I have some public group having same name like ASIA, USA and Austrailia so that this help me to pull autmatic all user from selected group on the bases of Region.


But In all this I am having problem that the dynamic fields or u can say merge fields are not filling please help me in this context. Thanking in advance for you help.




Asif Ahmed Khan

Sr. Software Eng.

Palmchip :)