+ Start a Discussion
naveen kunurunaveen kunuru 

mass email

can any plz help wats wrong with this code ,i am trying to send mass email to contact here is the code....
<apex:page Controller="emailclass">
<apex:form >
<apex:commandButton value="sendmail" action="{!sendemail}"/>

</apex:form>


</apex:page>


public with sharing class emailclass {
public list<id> contactids;
public emailclass(){
contactids=new list<id>();
for(contact con:[select id from contact limit 10])

{
contactids.add(con.id);
}
}
  public PageReference sendemail() {
    Messaging.MassEmailMessage mail=new Messaging.MassEmailMessage();
    mail.settargetobjectids(contactids);
    Messaging.sendemail(new Messaging.MassEmailMessage[]{mail});
 
        return null;
        }
        

}



error ;;;;;;;;;;;;;;;;;;;;;;;;;

Visualforce ErrorHelp for this Page
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing template id.: []
Error is in expression '{!sendemail}' in component <apex:commandButton> in page taskmassemail: Class.emailclass.sendemail: line 14, column 1
Class.emailclass.sendemail: line 14, column 1
SaranSaran
Hi Naveen,

You have to add the template of the email.

public PageReference sendemail() {
    Messaging.MassEmailMessage mail=new Messaging.MassEmailMessage();
    mail.settargetobjectids(contactids);
    mail.setTemplateId("Any of ur template ID that you have created under setup -> Communication Templates -> Email Templates");
    mail.setUseSignature(false);
    mail.setSaveAsActivity(false);
    Messaging.sendemail(new Messaging.MassEmailMessage[]{mail});
}