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
sowmya thotthadisowmya thotthadi 

how to send one mail to the multiple recipients where i am able to send only one recipient

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});
    }
   
  }
Tejas BodheTejas Bodhe
Create a list of Recipients Email Addresses and loop through it.
Example:

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

for(String recipient :recipientEmailList) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(recipient);
mail.setSubject(y);
mail.setHtmlBody(z);    
mails.add(mail);
}
Messaging.sendEmail(mails);
bhanu reddy 24bhanu reddy 24
thank you so much responding me but i got error can you plzz help me
User-added image
Tejas BodheTejas Bodhe
I am not sure but try removing extra space between Public String              x {get;set;}
sowmya thotthadisowmya thotthadi
can u explain that briefly
sowmya thotthadisowmya thotthadi
bcz i got error :
SendMailController Compile Error: Invalid identifier ''. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'. at line 9 column 43

public class SendMailController {

    public String z { get; set; }
    public Attachment attach{get ;set;}    
    public String y { get; set; }
    public list<String> x { get; set; }
   
    public void saveMethod() {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(String xxx : x){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          
        mail.setToAddresses(xxx);
        mail.setSubject(y);
        mail.setHtmlBody(z);    
        mails.add(mail);
    }
    Messaging.sendEmail(mails);
   }
  }
RD@SFRD@SF
Hi Soumya and Tejas,

1. Just found something weird. I tried copying and pasting the List declaration and I am able to recreate the error Soumya is getting.
2. Can you like re-write the list declaration code manually and try again. I suspect there is some encoding which is putting "." in the list identifier. Maybe because it was copy pasted from someplace. 

Hope it helps
RD
 
BHARAT KHANNA 16BHARAT KHANNA 16
Hi Sowmya,

Can you check the below code:

public class SendMailController {

    public String z { get; set; }
    public Attachment attach{get ;set;}    
    public String y { get; set; }
    public list<String> x { get; set; }
   
    public void saveMethod() {
        Messaging.SingleEmailMessage emails = new Messaging.SingleEmailMessage();
        emails.setToAddresses(x);
        emails.setSubject(y);
        emails.setHtmlBody(z);
        Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {emails};
        Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
   }
  }

 
Tejas BodheTejas Bodhe
Hi Sowmya,

Don't copy and paste the code directly.
I just tried my code in my Org.
There is some issue with encoding.
So don't copy and paste the code.
Please type whole code and it will definitely work.
Make sure to create a list of recipients before execution of a loop. 
Sorna JenefaSorna Jenefa
 
Hi Sowmya,

Refer the link below:

http://devendranatani.blogspot.com

http://www.salesforceadda.com/2017/05/send-emails-to-multiple-email-ids-using.html

http://forceguru.blogspot.in/2011/03/how-to-send-more-than-10-e-mails.html
 
Can you check the below code:

Apex class:

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

for(User portalUser :lstPortalUser) {

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

string body = 'Hi '+ portalUser.LastName;

mail.setSubject('Test Subject');

mail.setTargetObjectId(portalUser.Id); mail.setSaveAsActivity(false);

mail.setHtmlBody(body); mails.add(mail);

}

Messaging.sendEmail(mails);

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

Trigger Coding:

trigger Proposal on Contact (before insert) {

  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
  for (Contact myContact : Trigger.new) {
    if (myContact.Email != null && myContact.FirstName != null) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(myContact.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('sirdavid@bankofnigeria.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@bankofnigeria.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + myContact.FirstName + ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Thanks,
Jenefa 
Sweet Potato Tec