1

I am receiving the following exception when trying to send an email through Apex:

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

Apex code to send email:

    try {
        // Create an email invitation for everyone in the contacts list
        if (!contacts.isEmpty()) {
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (String contact : contacts) {
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                System.debug('TO ADDRESS: ' + new String[] {contact});
                email.setToAddresses(new String[] {contact});
                email.setOrgWideEmailAddressId('0D250000000TQTP') ;
                email.setSubject('Invitation');
                email.setHtmlBody('Please click the link to accept invitation: <a href="' + invitationURL + '">Invitation</a>' );
                emails.add(email);
            }  

        // Send the email invitations
        List<Messaging.SendEmailResult> sendEmailResults = Messaging.sendEmail(emails); 

        // Returns feedback message if email was sent successfully or not 
        for (Messaging.SendEmailResult result : sendEmailResults) {
            if (result.IsSuccess()) {
                emailSent = true;
                System.debug('Email sent successfully!');
            } else {
                System.debug('There was an error. The email was not sent.');
                System.StatusCode statusCode = result.getErrors()[0].getStatusCode();
                String errorMsg = result.getErrors()[0].getMessage();
                System.debug('Status code: ' + statusCode);
                System.debug('Error msg: ' + errorMsg);
            }
        }
    }

} catch (Exception e) {
    System.debug('Exception: ' + e);
}
}

Can anyone shine some light on this issue? It will send the email successfully when I hardcode the contact email like so email.setToAddresses(new String[] {'[email protected]'}) but not when I dynamically set the address.

EDIT:

    public void sendInvitations() {
    // Send out email invites to all contacts on list
    if (emailAddresses != null) {
        sendHigiInvite.sendEmail(emailAddresses, UserInfo.getSessionId());
        }
    }

contacts gets passed to this email method as a parameter List<String>. I check if it is null before passing it to the method. My log shows this as the value for contacts

10:22:03:007 USER_DEBUG [28]|DEBUG|CONTACTS: ("[email protected]", "[email protected]")

11
  • can you take one contact record from the query criteria and see if the user has access to the record
    – Rao
    Commented Sep 8, 2016 at 17:35
  • Where are you getting contacts? Are you sure it does not contain any null values?
    – Adrian Larson
    Commented Sep 8, 2016 at 17:36
  • 2
    Do you perhaps have the quotes in the included string? As in "[email protected]" instead of just [email protected]? Commented Sep 8, 2016 at 17:42
  • 1
    I am sure what sebastian pointed is what is happening, add this line after the start of the for loop and let us know if the error gets fixed contact = contact.replace('\"','');
    – Rao
    Commented Sep 8, 2016 at 17:52
  • 1
    @SebastianKessel please post your comment as an answer, great observation could have never gussed the "" getting added looking at the error message
    – Rao
    Commented Sep 8, 2016 at 17:58

1 Answer 1

2

Do you perhaps have the quotes in the included string? As in "[email protected]" instead of just [email protected]? That could be it.

2
  • Thank you! Would have never guessed the quotations were the issue
    – jalamak
    Commented Sep 8, 2016 at 18:03
  • 2
    You work in SFDC long enough, you see it all. :D Commented Sep 8, 2016 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .