public with sharing class SendEmailMultipleAttachments {
public List<Attachment> attachList{get;set;}
public List<SelectOption> numberList{get;set;}
public String emailValue{get;set;}
public String noofAttachments{get;set;}
public String emailBody{get;set;}
public Boolean rex{get;set;}
public SendEmailMultipleAttachments() {
numberList = new List<Selectoption>();
for(Integer i=0;i<5;i++){
String str =String.valueof(i);
numberList.add(new Selectoption(Str,str));
}
//attachList.add(new Attachment());
rex=true;
}
public void sendEmail(){
System.debug(attachList);
Messaging.SingleEmailMessage email = new
Messaging.SingleEmailMessage();
String [] list1= emailValue.split(',');
email.setToAddresses(list1);
email.setSubject('test');
email.setPlainTextBody(emailBody);
List<Messaging.EmailFileAttachment> emailAttachList = new
List<Messaging.EmailFileAttachment>();
for(Attachment att : AttachList){
Messaging.EmailFileAttachment emailAttach = new
Messaging.EmailFileAttachment();
emailAttach.setFileName(att.name);
emailAttach.setBody(att.body);
emailAttachList.add(emailAttach);
}
email.SetFileAttachments(emailAttachList);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
attachList=null;
}
public void fillList(){
attachList = new List<Attachment>();
System.debug('In' + noofAttachments);
for(Integer i=0 ; i < Integer.valueof(noofAttachments) ; i++){
attachList.add(new Attachment());
}
rex=false;
System.debug('In' + noofAttachments);
}
}
Visualforce PAge:
<apex:page tabStyle="Account"
controller="SendEmailMultipleAttachments">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1" id="repeat">
<apex:inputText label="Email:" value="{!emailValue}"
id="emailfield" />
<apex:selectList value="{!noofAttachments}" size="1" style="width:
250px" rendered="{!rex}" label="No Of Attachments" >
<apex:selectOptions value="{!numberList}" />
<apex:actionSupport event="onchange" action="{!fillList}"
reRender="repeat" />
</apex:selectList>
<apex:repeat value="{!attachList}" var="att" >
<apex:inputFile title="Attachment " value="{!att.Body}"
fileName="{!att.Name}" id="file" rendered="
{!if(noofAttachments!=null && noofAttachments!='',true,false)}"
/>
</apex:repeat>
</apex:pageBlockSection>
<apex:inputTextarea richText="true" value="{!emailBody}"/>
<apex:pageBlockButtons location="bottom" id="btn">
<apex:commandButton value="Send Email" action="{!sendEmail}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
apex:inputFile
inputs have all the sameid
. Try to remove theid
attribute.