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
siva@shiva.comsiva@shiva.com 

How to send multiple attachments

hi

 

 

<apex:page Controller="gmails" >

<script type="text/javascript">
function addFile(b) {
if (b && b.parentNode &&b.parentNode.insertBefore &&document.createElement) {
var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.name = 'filename2[]';

b.parentNode.insertBefore(fileInput, b);

b.parentNode.insertBefore(document.createElement(' br'), b);
}
}
</script>
 
 
  <script type="text/javascript">
    function generatenew()
{
var d=document.getElementById("div");
d.innerHTML="<p><input type='text' name='food'>";
}
</script>
<apex:form >


 <apex:pageblock title="Mailing to the" >
  <apex:pageblocksection columns="1">
  <apex:outputLabel value="Email ID" for="subject4"/>
  <apex:inputtext value="{!emails}"  id="subject4"/><br/>
 
 <apex:outputLabel value="subject" for="subject3"/>
 <apex:inputtext value="{!subject}"  id="subject3"/><br/>
 
 <apex:pageblockSectionItem >
 <apex:outputLabel value="attachment"  for="attachfile"/>
 <input type="file" name="filename2[]" id="attachfile"/>
 <input type="button" value="Add another file" onclick="addFile(this);"/>
</apex:pageblockSectionItem>


 
 <apex:outputLabel value="body" for="subject3"/>
 <apex:inputtextarea value="{!body}" rows="10"  cols="100"/>
 </apex:pageblocksection>
 <apex:commandButton value="send email" action="{!send}"/>
 
 </apex:pageblock>
 </apex:form>
</apex:page>

 

---------------------------

class

public with sharing class gmails
{
public string subject{get; set;}
public string body{get;set;}
public string emails{get;set;}
public Attachment attachment{get;set;}


public gmails()
{
}


public pagereference send()
{
string []to=emails.split(':',0);

/*List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name,Body,BodyLength from Attachment where ParentId=:attachment.parentid])
        {  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
            efa.setFileName(a.Name);
            efa.setBody(a.body);
            fileAttachments.add(efa);
        }
        
    */    
        
        
 /*     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        // Reference the attachment page and pass in the account ID
        Integer numAtts=[SELECT count() FROM attachment];
        system.debug('Number of Attachments Atts = '+ numAtts);
        List<Attachment> allAttachment = new List<Attachment>();
        allAttachment = [SELECT Id, Name, ContentType, Body FROM Attachment];
        // Create the email attachment
        //List<Messaging.Emailfileattachment> efa = new List<Messaging.Emailfileattachment>();
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        
        if(numAtts > 0){
                for (Integer i = 0; i < numAtts; i++){
                    system.debug(allAttachment[i].Name);
                    efa.setFileName(allAttachment[i].Name);
                    efa.setBody(allAttachment[i].Body);
                    efa.setContentType(allAttachment[i].ContentType);
                }
        }
      */
    
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setsubject(subject);
//email.setFileAttachments(fileAttachments);


//email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
      
email.setToAddresses(to );
email.setplaintextbody(body);
messaging.sendemailresult[]r=messaging.sendemail(new messaging.singleemailmessage[]{email});
return null;
}

 
}

 

 

here i am getting run time error  please try this.

SammyComesHereSammyComesHere

Pleas share the error!!

siva@shiva.comsiva@shiva.com

Error: gmails Compile Error: Duplicate variable: email (attempt to re-create the variable with type: SingleEmailMessage) at line 50 column 30