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
Philip GulanPhilip Gulan 

What is Apex Code to send mass email to List<Contact> by selecting an email template?

Hi, 

I want to write Apex code to send mass email to a selected contact list - List<Contact> - and select an email template.
Any help is apperciated.

Thanks
Harish RamachandruniHarish Ramachandruni
 
Hi ,


You can use this code .
 
public class testemail
{
  private final List<Id> contactids ;
  public List<Contact> con ;
  public testemail()
  {
     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 });
  }  


}

Any issue ask me .


Regards ,
harish.R


 
Harish RamachandruniHarish Ramachandruni
Hi,
 
public class  sendemailapex
{
  public String RES { get; set; }
public sendemailapex()
{
con=new contact();
}
     public PageReference refresh() {
        return null;
    }
public String body { get; set; }
public String Folder { get; set; }
public list<selectoption> getdw()

{
  List<SelectOption> options = new List<SelectOption>();
     List<Folder> Folders = [Select Id, Name From Folder Where type = 'Email' ];
for(Folder F : Folders)
 {  
options.add(new SelectOption(F.Name, F.Name));
 //options.add(new selectOption('F.Unified public Email Template','F.Unified public Email Template'));
    //options.add(new selectOption('F.My Personal Email Template ','F.My Personal Email Template'));
    }
    return options;
   }
public String Cc { get; set; }
public String subject { get; set; }
public contact con{ get; set; }
public  string template{get;set;}

  public list<selectoption> getName() 
  {
  
  List<SelectOption> options = new List<SelectOption>();
List<EmailTemplate> et = [select id,name from EmailTemplate where Folder.Id = :RES];
     for( EmailTemplate  e : et)
   {
      options.add(new SelectOption(e.id, e.Name));
       }
    return options;
    }
   public void send()
{
con=[select id,lastname,Email  from contact where id='0032800000RC1pr'];
}
} 
Vf page 
<apex:page controller="sendemailapex">
  <apex:form >
  <apex:pageBlock >
   <apex:sectionHeader title="Sending email in apex"/>
   <apex:pageBlockSection title="Contact Details" columns="1">
   <apex:outputField value="{!con.Lastname}"/>
   <apex:outputField value="{!con.email}"/>
  My Folder:<apex:selectList size="1" value="{!Folder}">
  <apex:selectOptions value="{!dw}"></apex:selectOptions>
  <apex:actionSupport event="onchange" action="{!refresh}"  reRender="r1"/>
  </apex:selectList>
  {!RES}
Template:<apex:selectList size="1" value="{!template}">
  <apex:selectOptions value="{!name}"></apex:selectOptions>
   </apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection title="Output Details of Template" columns="1">
 <apex:outputText value="{!Cc}" label="Cc"></apex:outputText>
<apex:outputText value="{!subject}" label="Subject"></apex:outputText>
<apex:outputText value="{!body}" label="Body"></apex:outputText>
</apex:pageBlockSection>
<apex:commandButton action="{!send}" value="SEND"/>
</apex:pageBlock>
</apex:form>
</apex:page>

this is single but selection templte is available modify above and last time code .


Regards,
Harish.R.
Philip GulanPhilip Gulan
Hi Harish,

I have prepared the following code by help of a friend and it is working fine.
I need to know how I can pass the template ID instead of giving a hardcode, so that the admin can change the seleted email template.
 
global class myEmailClass
{
    webservice static void emailContact(list<id> conId)
    { 
       List<Contact> contactNames = [SELECT name, email FROM contact WHERE id IN : conId];
       Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
       mail.setTargetObjectIds(conId);
       mail.setTemplateId('00X41000000Ivp1');
       Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
      }  
   }

 
Bhanu MBhanu M
By querying EmailTemplate object with name we can get TemplateId

EmailTemplate et = [SELECT Id, Name FROM EmailTemplate WHERE Name = 'Lead Email Template' LIMIT 1];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTemplateId(et.Id);