+ Start a Discussion
Shravani M 9Shravani M 9 

Hi EveryOne. I have created the page with a controller From where user able to send Email. But my requirement is the user what to entered the multiple emails from the vf page . How can I do this

Best Answer chosen by Shravani M 9
brahmaji tammanabrahmaji tammana
Here is the code:

To work this code, User has to enter multiple addresses by seperating with semicolon.
For example:
abc@xyz.com
def@xyz.com
To send an email to these two emails, User has to enter email in the following format.

abc@xyz.com;def@xyz.com

I have made three changes (actually only two). 
public PageReference send()
    {
        String[] toaddress = new String[]{sEmail}; //Take the email in this String sEmail
        String[] arrTest = sEmail.split('\\;');  //Split with semicolon and the result will be stored in array
        System.debug('Email :' + toaddress[0]);
        
       // List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
         Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
        
            email.SetToAddresses(arrTest ); //add the array in to address
            email.SetSubject(Subject);
            email.SetPlainTextBody(Emailbody);

I actually splitted the email address by semi colon and using them in sending an email.

Hope it should work.

If it works, do not forget to mark it as Best Answer :)

Happy Coding !!

Regards,
Brahma

All Answers

brahmaji tammanabrahmaji tammana
Hi Shravani,

Just paste your code here. I will try to help you.

Thanks
Brahma
Shravani M 9Shravani M 9
Hi Brahma,

Here is my controller:


public class Send_Docs_To_Customer {
    
    
    public String Subject       {get;set;}
    public String Emailbody     {set;get;}
    private final Work_Order__c WO;
    public String Email         {get;set;}
    public String[] toAddresses;

    
    public Send_Docs_To_Customer(ApexPages.StandardController controller) {
        
      try{
          
    Work_Order__c wo    = [
                        Select  Id,
                                Contractor__c
                        FROM    Work_Order__c
                        WHERE   id =: apexpages.currentpage().getparameters().get('ID')
                        ];
    System.Debug(wo);
    
    Account acct  =   [
                        SELECT  ID,
                                Name,
                                Project_Management_Lead_Email__c
                        FROM    Account
                        WHERE   ID =: wo.Contractor__c
                            ];
    
    Email = acct.Project_Management_Lead_Email__c;
    System.Debug('Account Email:' +Email);      
   
      }
      
     catch(exception e){
        
    System.Debug('Error message'+e.getMessage());
    System.Debug('Error Line num'+e.getLineNumber());
    }
      
   
}
    
    public Work_Order__c getWO() {

        return WO;

    }

    
    public PageReference send()
    {
        String[] toaddress = new String[]{Email};
        
        System.debug('Email :' + toaddress[0]);
        
       // List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
         Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
        
            email.SetToAddresses(toaddress); 
            email.SetSubject(Subject);
            email.SetPlainTextBody(Emailbody);
            
           // emails.add(email);
    
        try{
           // Sends the email
        Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
        
        //Messaging.sendEmail(emails);
        }
        
        catch(exception e){
        system.debug('Error message'+e.getMessage());
        system.debug('Error Line num'+e.getLineNumber());
        }
        
        return Page.WO_Message;
                
    }
}


VF PAGE:
<apex:page standardController="Work_Order__c" extensions="Send_Docs_To_Customer" sidebar="false" showHeader="false">
    <apex:messages />
     <apex:form >
    <apex:pageBlock title="Send an Email to Your">
        
        <apex:pageBlockSection columns="3">
            
            <apex:pageBlockSectionItem >                   
                    <apex:outputText value="Name:"/>
                    <apex:inputText value="{!Work_Order__c.Name}"/>       
            </apex:pageBlockSectionItem>
        
            <apex:pageBlockSectionItem >                   
                    <apex:outputText value="Email:"/>
                    <apex:inputText value="{!Email}"/>       
            </apex:pageBlockSectionItem>
            
            </apex:pageBlockSection>
   
        <br /><br />
            <apex:outputLabel value="Subject" for="Subject"/>:<br />     
            <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
            <br /><br />
            <apex:outputLabel value="Body" for="Body"/>:<br />     
            <apex:inputTextarea value="{!emailbody}" id="Body"  rows="10" cols="80"/>           
            <br /><br /><br />
            <apex:commandButton value="Send Email" action="{!send}" /> 
                
            </apex:pageblock>
        </apex:form>
</apex:page>

Here there is " EMAIL " section. So when user enter Emails from VF Page then we need to send mails to email address.
brahmaji tammanabrahmaji tammana
Here is the code:

To work this code, User has to enter multiple addresses by seperating with semicolon.
For example:
abc@xyz.com
def@xyz.com
To send an email to these two emails, User has to enter email in the following format.

abc@xyz.com;def@xyz.com

I have made three changes (actually only two). 
public PageReference send()
    {
        String[] toaddress = new String[]{sEmail}; //Take the email in this String sEmail
        String[] arrTest = sEmail.split('\\;');  //Split with semicolon and the result will be stored in array
        System.debug('Email :' + toaddress[0]);
        
       // List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
         Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
        
            email.SetToAddresses(arrTest ); //add the array in to address
            email.SetSubject(Subject);
            email.SetPlainTextBody(Emailbody);

I actually splitted the email address by semi colon and using them in sending an email.

Hope it should work.

If it works, do not forget to mark it as Best Answer :)

Happy Coding !!

Regards,
Brahma
This was selected as the best answer
Shravani M 9Shravani M 9
Thank you so much Its working