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
mpiotrompiotro 

how to put multiple email addresses in the "to" field of an outbound email

Hi,

 

I think I read in the apex developer's guide that it is possible to have up to 10 (or 25?) email addresses in the "to" field of the mail object for a single outgoing email.  All of the examples I have found show just a single address, something like this:

 

OutMail.setToaddresses('joe.blow@acme.com');

 

Ideally, I'd like to be able to send an email to anywhere from 3-8 addresses.  I am able to send to 3 addresses, by assigning one email field to the setToaddresses,  one to the setCcaddresses, and one to the setBccaddresses.

 

Also, it appears to me that only fields created as an email type work.  I've tried to create a text field and put in multiple email addresses (ex 'joe@acme.com', bill@acme.com') and assign this to the setToaddresses mail object, but no dice. 

 

Right now, I feel like I'm limited to 3 email addresses.  Does anyone know how to combine multiple email addresses (from individual custom fields or one big text field) into the setToaddresses mail object?

Best Answer chosen by Admin (Salesforce Developers) 
ColinKenworthy2ColinKenworthy2

I dont think putting a single text field into a String[] will turn it into an array with multiple entries. You could always display addr.size() to see if it contained > 1 entry. Also leading/trailing white space might invalidate an email address.

If you use a single text field you wll need to split it up. See the split() method on String.

 

 

Have you tried using two or more text fields with one email in each?

 

String[] addr = new String[]{}; // max 10 addresses addr.add(RMS2.test1__c);

addr.add(RMS2.test2__c);

OutMail.setToAddresses(addr);

 

 

 

All Answers

ColinKenworthy2ColinKenworthy2

Put the email addresses into a String[], then use that in the setToAddresses() method.

 

 

 

String[] addr = new String[]{'a@b.com','c@d.com','e@f.com'}; // max 10 addresses

OutMail.setToAddresses(addr);

 

 

 

mpiotrompiotro

Colin,

Thanks for the reply.

Here's how I've got it coded:

                String[] toAddresses = new String[] {RMS2.test__c};
                 OutMail.setToaddresses(toAddresses);

 

I've tried this way, but it won't work.  I get the INVALID_EMAIL_ADDRESS, Invalid to address error message.

Right now, I have a custom text field called test.  In this field I have put in 2 email addresses, and whenever I use this field as my mail object to field, I get an error. I have tried separating the emails by a comma, by a semi-colon; I have put the email addresses in directly as is, within single quotes, within double quotes, etc...am I missing something else here?

ColinKenworthy2ColinKenworthy2

I dont think putting a single text field into a String[] will turn it into an array with multiple entries. You could always display addr.size() to see if it contained > 1 entry. Also leading/trailing white space might invalidate an email address.

If you use a single text field you wll need to split it up. See the split() method on String.

 

 

Have you tried using two or more text fields with one email in each?

 

String[] addr = new String[]{}; // max 10 addresses addr.add(RMS2.test1__c);

addr.add(RMS2.test2__c);

OutMail.setToAddresses(addr);

 

 

 

This was selected as the best answer
mpiotrompiotro

Bingo, that works!  Thanks so much for your help!!

 

-Mike