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
ChrisMagellanChrisMagellan 

SingleEmailMessage not taking updated

Hi guys,

I'm pulling my hair out with this one, as almost identical code is working fine in another class!

I'm trying to run the below code and I'm getting a consistent error: FATAL_ERROR|System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []

When I debug the toAddresses list and the whoel mail message it's not taking any of the inputs!

global class NBAAForward implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
 
  Messaging.reserveSingleEmailCapacity(1);
 
  Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  String[] toAddresses = new String[] {'nickc@magellanjets.com'};
  String[] ccAddresses = new String[] {email.fromAddress};

  mail.setToAddresses(toAddresses);
  system.debug(mail.toAddresses);
  mail.setCcAddresses(ccAddresses);
 
  mail.setSenderDisplayName('Magellan Jets');
 
  mail.setSubject(email.Subject);
 
  mail.setBccSender(false);
 
  mail.setUseSignature(false);
 
  mail.setPlainTextBody('PLEASE REPLY TO ' + email.fromAddress + '\n \n' + email.plainTextBody);
  system.debug(mail);
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  
  return result;
}

public static testmethod void testEmail() {
 
  // create new email for testing
  Messaging.InboundEmail email = new Messaging.InboundEmail();
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
 
  email.subject = 'test nbaa post';
  env.fromAddress = 'chris@magellanjets.com';
 
  email.plainTextBody = 'TEST NBAA POST';
 
  NBAAForward emailServiceObj = new NBAAForward();
  emailServiceObj.handleInboundEmail(email, env);
 
}

}
Best Answer chosen by ChrisMagellan
MaxPowerForceMaxPowerForce
Your test is setting the envelope from address, but not the from address on the InboundEmail object.  Try changing this part of the unit test:

env.fromAddress = 'chris@magellanjets.com';

to:

email.fromAddress = 'chris@magellanjets.com';

and the test will pass.