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
bhanu reddy 24bhanu reddy 24 

i am unable to send a mail to multiple recipients from 'To' field

// sending email to only one person

public class SendMailController {

    public String z { get; set; }

    public String y { get; set; }

    public String     x { get; set; }

    public void saveMethod() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        string[] xyz =new string[] {x};  
        mail.setToAddresses(xyz);
        mail.setSubject(y);
        mail.setHtmlBody(z);
          
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
   
  }


// visual force page

<apex:page controller="SendMailController" sidebar="false">
  <apex:form >
   <apex:pageBlock >
    <apex:pageBlockSection >
     <apex:outputLabel value="To"/>
     <apex:inputText value="{!x}" />
   
     <apex:outputLabel value="Subject"/>
     <apex:inputText value="{!y}"/>
     
     <apex:outputLabel value="Body"/>
     <apex:inputTextarea value="{!z}"/>
      
    </apex:pageBlockSection> 
     <apex:pageBlockButtons location="bottom"> 
       <apex:commandButton value="Send" action="{!saveMethod}" />
     </apex:pageBlockButtons>
     
   </apex:pageBlock>
  </apex:form>
</apex:page> 
Best Answer chosen by bhanu reddy 24
Waqar Hussain SFWaqar Hussain SF
Ok then you can use your first code and you can write the email addresses separated by comma. And will have to update the code slightly.

See updated code
public class SendMailController {

    public String z { get; set; }

    public String y { get; set; }

    public String     x { get; set; }

    public void saveMethod() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> lstEmail = x.split(',');
        mail.setToAddresses(lstEmail);
        mail.setSubject(y);
        mail.setHtmlBody(z);
          
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
   
  }

 

All Answers

Waqar Hussain SFWaqar Hussain SF
Create multiple input field to set multiple email addresses.
See
 
public class SendMailController {

    public String z { get; set; }

    public String y { get; set; }

    public String     x1 { get; set; }
    public String     x2 { get; set; }
    public String     x3 { get; set; }

    public void saveMethod() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        string[] xyz =new string[] {x1}; 
        xyz.add(x2); 
        xyz.add(x3); 
        mail.setToAddresses(xyz);
        mail.setSubject(y);
        mail.setHtmlBody(z);
          
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
   
  }



// visual force page
 
<apex:page controller="SendMailController" sidebar="false">
  <apex:form >
   <apex:pageBlock >
    <apex:pageBlockSection >
     <apex:outputLabel value="To1"/>
     <apex:inputText value="{!x1}" />
   
     <apex:outputLabel value="To2"/>
     <apex:inputText value="{!x2}" />

     <apex:outputLabel value="To3"/>
     <apex:inputText value="{!x3}" />


     <apex:outputLabel value="Subject"/>
     <apex:inputText value="{!y}"/>
     
     <apex:outputLabel value="Body"/>
     <apex:inputTextarea value="{!z}"/>
      
    </apex:pageBlockSection> 
     <apex:pageBlockButtons location="bottom"> 
       <apex:commandButton value="Send" action="{!saveMethod}" />
     </apex:pageBlockButtons>
     
   </apex:pageBlock>
  </apex:form>
</apex:page>

 
bhanu reddy 24bhanu reddy 24
it is not working i got error 
iam asking one (To named field )i will write multiple  email address how to send at a time
can you plzz write a program
Waqar Hussain SFWaqar Hussain SF
Ok then you can use your first code and you can write the email addresses separated by comma. And will have to update the code slightly.

See updated code
public class SendMailController {

    public String z { get; set; }

    public String y { get; set; }

    public String     x { get; set; }

    public void saveMethod() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> lstEmail = x.split(',');
        mail.setToAddresses(lstEmail);
        mail.setSubject(y);
        mail.setHtmlBody(z);
          
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
   
  }

 
This was selected as the best answer
GhanshyamChoudhariGhanshyamChoudhari
List<string> emaillst =new List<string>();
emaillst.add('xx@xxx.com');
emaillst.add('yy@xxx.com');
message.toAddresses = emaillst;

 
bhanu reddy 24bhanu reddy 24
thank you so much