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
    Could you please provide the visualforce page code and some description about what it isn't working? – Martin Lezer Jan 2 '18 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 Jan 2 '18 at 9:42
  • 1
    This is probably because your apex:inputFile inputs have all the same id. Try to remove the id attribute. – Martin Lezer Jan 3 '18 at 8:05
-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.

  • it doesn't work. – fdsfad Jan 2 '18 at 13:14
  • What kind of error message you got? – Venkatesh Jan 2 '18 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 – fdsfad Jan 2 '18 at 13:36
  • attachment body and name are not binding in controller. – fdsfad Jan 2 '18 at 13:36
  • I edited the answer, this may be helpful for you. – Venkatesh Jan 3 '18 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.