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
Amit Kumar Singh 19Amit Kumar Singh 19 

Mass Email using Custom label : Test Class Issue



Hi Team,

I want to send email to number of users, for this I am storing 'toaddress' in custom label.
here is the code,
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

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

        List<String> sendTo = new List<String>();
        sendTo.add(Label.CommunityContact);
        mail.setToAddresses(sendTo);
        
        mail.setReplyTo('noreply@salesforce.com');
    
        List<String> ccTo = new List<String>();
        mail.setCcAddresses(ccTo);
        
        String Sub = 'Application Submitted for Group : ' + groupname;

        mail.setSubject(Sub);
        String body = 'An application for group membership has been submitted with the following information:<br />';
      
        body += '<p><b>Name</b>: ' + firstName + ' ' + lastName + '<br>';
        body += '<b>Company:</b> ' + company + '<br>';
        body += '<b>Address:</b> ' + streetAddress + ' ' + streetAddressLine2 + '<br>';
        body += '<b>City:</b> ' + city + '<br>';
        body += '<b>State/Province/Region:</b> ' + stateProvinceRegion + '<br>';
        body += '<b>Postal/Zip Code:</b> ' + postalZip + '<br>';
        body += '<b>Country:</b> ' + country + '<br>';
        body += '<b>Email:</b> ' + email + '<br>';
        body += '<b>Job Title:</b> ' + jobTitle + '<br>';
        body += '<b>Regional Account Manager Name:</b> ' + AMFirstName + ' ' + AMLastName + '<br>';
        body += '<b>Disributor:</b> ' + Dist + '<br></p>';
        
        mail.setHtmlBody(body);
    
        mails.add(mail);
        Messaging.sendEmail(mails);

I am getting test class error shown below:-

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: amit@abc.com;sudeep@abc.com: [toAddresses, amit@abc.com;sudeep@abc.com] 
Stack Trace: Class.GroupRegistrationController.registerUser: line 95, column 1 Class.GroupRegistrationControllerTest.testGroupRegistrationController: line 40, column 1


Please help me to resolve this issue.

Thanks,

 
Best Answer chosen by Amit Kumar Singh 19
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Amit, 

I think, the problem is due to worng email address.
You can split the email address based on the delimeter ';' and cann all to the list. 
You can try this one - 
List<String> sendTo = new List<String>();
sendTo.addAll((Label.CommunityContact).split(';'));
mail.setToAddresses(sendTo);
Please, let me know, if it helps you.

Thanks, 
Sumit Kumar Singh

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Amit, 

I think, the problem is due to worng email address.
You can split the email address based on the delimeter ';' and cann all to the list. 
You can try this one - 
List<String> sendTo = new List<String>();
sendTo.addAll((Label.CommunityContact).split(';'));
mail.setToAddresses(sendTo);
Please, let me know, if it helps you.

Thanks, 
Sumit Kumar Singh
This was selected as the best answer
doravmondoravmon
setToAddresses is supposed to take a string[] param, but you use sendTo.Add(label), this is just one space in the string[] -- sendTo[0] = labe.
So you need to convert the label to string array first.
Amit Kumar Singh 19Amit Kumar Singh 19
Thanks a lot Sumit and doravmon.

It is working fine now.