I have a custom object that allows my users to create series of emails to their clients. I have a scheduled Apex job running hourly that scans for emails that are schedule to go out, creates the EmailMessages and sends them out. I have fields for recipient lists and options to use the Contact as Target Object id.

This setup has been working for 5 years, and suddenly stopped working for emails that have a target object id, but no additional recipents. Has something changed in the new release?

Here's how I set up the Email Message:

public static Messaging.SingleEmailMessage createMessage(Email__c e){
    List<String> recipients = e.Email_List__c.split(',');
    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    if(!recipients.isEmpty()) msg.setToAddresses(recipients);
        System.debug('target object id:'+e.TargetObjectForEmail__c);
    if(e.From_Triggering_User__c == True){
    else if(e.OrgWideEmail__c != ''){
    System.debug('adding message. The target object id is:'+msg.getTargetObjectId());
    return msg;

And here's how I send them:

List<Messaging.SendEmailResult> results = Messaging.sendEmail(emailsToSend, false);
            Integer numResults = results.size();
            String errorMessage;
            System.StatusCode statusCode;
            //loop through results
            for(Integer i = 0; i < numResults; i++){
                //get the result and email object
                Messaging.SendEmailResult r = results.get(i);
                Email__c e = preparedEmails.get(i);
                if (r.isSuccess()) {
                    e.Sent_Date__c = datetime.now();
                    statusCode = r.getErrors()[0].getStatusCode();
                    errorMessage = '';
                    for(Messaging.SendEmailError error:r.getErrors()){
                     errorMessage += error .getMessage() + '\r\n';
                    errorMessageReport += '\r\n' + errorMessage + '\r\nLink: ' +
                    System.Url.getSalesforceBaseURL().toExternalForm()  + '/' + e.id
                    + '\r\nThe recipient list: ' + e.Email_List__c;

The error is: Invalid to address :

I have triple checked that the contacts have valid email addresses.

Does anyone know what could be the issue?

  • As per Salesforce docs: Do not specify the IDs of records that have the Email Opt Out option selected. Is there any record which have that option selected? – CloudConnect Nov 29 '19 at 14:24
  • Thanks for having a look @CloudConnect, but no, these contacts haven't opted out. – hgolov Dec 1 '19 at 8:21

In case anyone comes across this, here's what happened.

List<String> recipients = e.Email_List__c.split(',');

The line above created a list with one empty string. So even though I checked if the list was empty before setting the toAddresses to it here:

if(!recipients.isEmpty()) msg.setToAddresses(recipients);

it was adding one empty email address.

I updated it to the following:

        List<String> recipients = e.Email_List__c.split(',');

and that solved the issue.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.