+ Start a Discussion
@ M  Coder@ M Coder 

issue in setCCAddresses to send emails in CC --URGENT!!!!!

Hello Guys , 
Basically i need to pass email ids present in my custom metadattype to CC in mailing logic when ever my label is IND .
in simple i need to emails  cc (those email id.) to users.

i just kept my piece of code whether i am getting the below error 

My custom metadatype contains two values 
1) label: Ind  2) Email_Address__c = ram@gmail.com sam@gmail.com hello@gmail.com
My piece of code  
  //calling custom metadata type
   map<string,string> emailmap = new map<string,string>();
   for(Email__mdt ce : [SELECT Label,Email_Address__c FROM Email__mdt])
   emailmap.put(ce.Label , ce.Email_Address__c);
   //Splitting CC email addresses and adding in array
     String[] ccAddresses = new String[]{};
    system.debug('Map has values....');
    for(string ev:emailmap.Values())
        list<string> emailid = ev.split(',');
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setWhatId(leadRecId );
       Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

Error I am getting: 
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : ram2929@gmail.com sam@gmail.com hello@gmail.com 

My analysis: i think split string is not working fine in my logic , can anyone please modify the piece of code.

Andrew GAndrew G
if your metadata holds a string like:
ram@gmail.com sam@gmail.com hello@gmail.com

i notice that you have spaces as separators, not commas (,)

tweak your code to allow for space, not comma

run the below in dev console to review the output:
String emailString = 'ram@gmail.com sam@gmail.com hello@gmail.com';
System.debug('original: '+ emailString);
list<String> emailStrings = emailString.split(' ');
for(String s : emailStrings) {
    System.debug('s: ' + s);