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 Jadhav 13Amit Jadhav 13 

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: : [toAddresses, ]

I'm stuck in error when validating changeset in production when in run test there is test coverage 89% please help me on that
Apex Code
public void finish(Database.BatchableContext bc){
        string emailbody = '' ;
            List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.saveAsActivity = false;
            mail.toAddresses = new String[] {emailAdd};
            mail.setSenderDisplayName('Marketing Lead Error');
            mail.setSubject('Adding Campaign Memeber');
            emailbody = 'Hi '+userName +',<br/><br/> Error occured while adding Campaign Member. Please visit <a href="' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + err.ID +'">this link</a> for more information';
            mail.setHtmlBody(emailbody);
            mails.add(mail);
            Messaging.sendEmail(mails);
            
        }
    }

handler :
global class AddCampaignMemberEmailHandler implements Messaging.InboundEmailHandler {
    Map < String ,Integer> headersMap = new Map < String ,Integer>();
    //integer abc = 1/0;
    Set<String> emailset =  new Set<String>();
    List<CampaignMember> cm = new List<CampaignMember>(); 
    Map<String,Id> campNameVsId = new  Map<String,Id>();
    public Map<string,integer> fieldNumberMap  = new Map<string,integer> ();
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        system.debug('Inbound Email class invoked'); //Added
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        Messaging.InboundEmail.BinaryAttachment[] tAttachments = email.binaryAttachments;
        String csvAsString = '';
        string strUserName = email.plainTextBody.substringBefore('\n');
        List<User> lstUser = new List<User>();
        string strEmail='';
        string UserName ='';
        lstUser= [SELECT Id,Email,name
                            FROM User 
                            WHERE Name = : strUserName 
                            LIMIT 1];
        system.debug('Name'+lstUser);
        if(lstUser.size()>0){
           strEmail = lstUser[0].Email; 
           UserName = lstUser[0].name;
         }
        for(Messaging.InboundEmail.BinaryAttachment btt: tAttachments){
            if (btt.filename.endsWithIgnoreCase('.csv')) {
                system.debug('before ' +btt.body.toString() );
                
                csvAsString =btt.body.toString() ;//blobToString(btt.body,'UTF-8');//btt.body.toString();
                System.debug('csvAsString' +csvAsString);
                String row = csvAsString.subString(0, csvAsString.indexOf('\n'));
                csvAsString = csvAsString.subString(csvAsString.indexOf('\n') + '\n'.length(),csvAsString.length());
                System.debug('row :' +row);
                getHeader(row);   
                System.debug('fieldNumberMap>>>>>>' +fieldNumberMap);
                  AddCampaignMemberBatch addcamp= new AddCampaignMemberBatch(csvAsString,fieldNumberMap,tAttachments[0].body,strEmail,btt.fileName,UserName); //Updated by pratik With User Mail id

                Database.executeBatch(addcamp,200); 
            }
        }
        return result;
    }
    
    private void getHeader(string row){
        string[] csvFieldNames = row.split(',');
        System.debug('csvFieldNames' +csvFieldNames);
        for (Integer i = 0; i < csvFieldNames.size(); i++) {
            csvFieldNames[i] = csvFieldNames[i].remove('"');
            fieldNumberMap.put(csvFieldNames[i].trim(), i);
            System.debug('fieldNumberMap' +fieldNumberMap);
        }
    }
    
    public static String blobToString(Blob input, String charset){
        final String hex = EncodingUtil.convertToHex(input);
        final Integer size = hex.length() >> 1;
        final List<String> bytes = new String[size];
        
        for (Integer i = 0; i < size; ++i) {
            bytes.set(i, hex.mid(i << 1, 2));
        }
        return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), charset);
    }
}

Test class :
@istest
public class AddCampaignMemberEmailHandlerTest {
    
    public static void sendEmail(String str){
        
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        Messaging.InboundEmail.BinaryAttachment inboundAttachment = new Messaging.InboundEmail.BinaryAttachment();
        
        email.subject = 'testsub';
        email.fromAddress = 'test@gmail.in';
        email.plainTextBody = ('Pratik D');   //Added
        email.toAddresses =new String[] {'pratikd@proximabiz.com'};
        inboundAttachment.body = blob.valueOf(str);
        inboundAttachment.fileName = 'Email_Attachment.csv';
        
        
        email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { inboundAttachment }; 
            
            // create an instance of the EmailToSalesForce class and test it with the data in the testMethod
            AddCampaignMemberEmailHandler  addcampaignHandler = new AddCampaignMemberEmailHandler();
        addcampaignHandler.handleInboundEmail(email, env ); 
        
        
    }
    
    
    public static testMethod void LeadInsert(){
        string str = 'Campaign Name,First Name,Last Name,Email Address,Job Title,Company,Street,City,State,Country,Zip,Linkedin Id,Github Id,Phone,Industry,Lead Source,Lead Source Detail,Notes \n'
            +'TestCampaign,Jubbi,Pearce,sneh@gmail.com,PM,Splunk,,Plano,Texas,USA,,zbpears,,,Software,,,Tests notes \n'
            +'TestCampaign,Julia,Pearce,jul@sp.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests notes" \n'
            +'TestCampaign,TestData,Pearce,hal.jordan@example.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests notes" \n';
        
        
        TestFactory.getAccount();
        TestFactory.getContact();
        TestFactory.getCampaign();
        TestFactory.getUser();
        //TestFactory.getCampaignMember();
        Test.startTest();
        sendEmail(str);
        Test.stopTest();
        System.AssertEquals(2,[Select id From Lead].size() );
        
        
        
    }
    
    Public static testMethod void AlreadyMember(){
        string str = 'Campaign Name,First Name,Last Name,Email Address,Job Title,Company,Street,City,State,Country,Zip,Linkedin Id,Github Id,Phone,Industry,Lead Source,Lead Source Detail,Notes \n'
            +'TestCampaign,Jubbi,Pearce,jb@test.org,PM,Splunk,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests ,notes" \n'
            +'TestCampaign,Julia,Pearce,jul@sp.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests,notes" \n'
            +'TestCampaign,TestData,Pearce,hal.jordan@example.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests notes" \n';
        
        
        TestFactory.getAccount();
        TestFactory.getContact();
        TestFactory.getCampaign();
        TestFactory.getCampaignMember();
        TestFactory.getUser();
        Test.startTest();
        sendEmail(str);
        Test.stopTest();
        System.assertEquals(2,[Select ContactId From CampaignMember WHERE ContactId != null].size());
    }
    
    public static testMethod void Errolog(){
        //Sending Wrong Email 
        string str = 'Campaign Name,First Name,Last Name,Email Address,Job Title,Company,Street,City,State,Country,Zip,Linkedin Id,Github Id,Phone,Industry,Lead Source,Lead Source Detail,Notes \n'
            +'TestCampaign,Jubbi,Pearce,jb@test.com,PM,Splunk,,Plano,Texas,USA,,zbpears,,,Software,,,Tests notes \n'
            +'TestCampaign,Julia,Pearce,j@test.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests notes" \n'
            +'TestCampaign,TestData,Pearce,b@test.com,PM,Test,,Plano,Texas,USA,,zbpears,,,Software,,,"Tests notes" \n';
        
        TestFactory.getCampaign();
       Test.startTest(); 
        sendEmail(str);
      Test.stopTest();  
        
    }
    
    
    
    
    
}