0
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>
3
  • 3
    Could you please provide the visualforce page code and some description about what it isn't working? Commented Jan 2, 2018 at 9:41
  • 1
    you could maybe share your visualforce page as well. And possible error messages that you have gotten or behaviors that you may have observed.
    – Nielsm
    Commented Jan 2, 2018 at 9:42
  • 1
    This is probably because your apex:inputFile inputs have all the same id. Try to remove the id attribute. Commented Jan 3, 2018 at 8:05

1 Answer 1

-1

For that error you have to reRender the whole "apex:form", not only the "apex:pageBlockSection". That is the mistake you done.

So remove id='repeat' on "apex:pageBlockSection" like following, apex:pageBlockSection columns="1"

Then add that on the "apex:form" like following, apex:form id="repeat"

Here you will get following error, "Rerender is not currently supported with rich text editing enabled"

Because, if the rich text area is "true", then we can't re-render that. If the rich text area is "false", then your attachment values will populate correctly. Hence you have to identify a different way for that one.

5
  • it doesn't work.
    – Ayaan
    Commented Jan 2, 2018 at 13:14
  • What kind of error message you got?
    – Venkatesh
    Commented Jan 2, 2018 at 13:33
  • System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments] Error is in expression '{!sendEmail}' in component <apex:commandButton> in page vcomp:sendemailmultipleattachments: Class.Vcomp.SendEmailMultipleAttachments.sendEmail: line 41, column 1
    – Ayaan
    Commented Jan 2, 2018 at 13:36
  • attachment body and name are not binding in controller.
    – Ayaan
    Commented Jan 2, 2018 at 13:36
  • I edited the answer, this may be helpful for you.
    – Venkatesh
    Commented Jan 3, 2018 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .