I've written a method which should send messages to the current user, the System Administrator, and a list of other addresses that I specify.

public static void sendErrorEmail(String body)
        Messaging.SingleEmailMessage errorMail = new Messaging.SingleEmailMessage();
        errorMail.setSubject('Error E-mail Subjectline');
        String[] sendTo = new String[]{};
        Profile adminProfile = [Select Id, Name from Profile where Name = 'System Administrator' LIMIT 1];
        if(UserInfo.getProfileId() != adminProfile.Id) 
            sendTo.add([Select Email, ProfileId from User where ProfileId = :adminProfile.Id LIMIT 1].Email);



        Messaging.SendEmailResult[] eResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{errorMail}, false);

However, this is giving me the following error (copied from debug log):


I've tried this without specifying setTargetObjectId as well, as it should not be necessary given that I am not using a template, but the same error occurs. When I checked the debug log to make sure that the target object and sendTo list were not null, they both appeared correct.

Anyone have ideas?

  • What error did you get when you commented errorMail.setTargetObjectId(UserInfo.getUserId()); out? I feel like you shouldn't have that line in your code without a template.
    – dphil
    Sep 11 '14 at 17:27
  • Exactly the same error. I'm just as confused as you, @dphil Sep 11 '14 at 17:37
  • What happens when you comment out this line: errorMail.setTargetObjectId(UserInfo.getUserId()); and the debug right below it
    – greenstork
    Sep 11 '14 at 17:44
  • This would seem to indicate that the problem could be with a bad email address on one of your recipients: developer.salesforce.com/forums/…
    – greenstork
    Sep 11 '14 at 17:51
  • Got it- if body = null, this error was thrown. Which is really bizarre error handling on Salesforce's part. Sep 11 '14 at 17:58

According to your code snippet you are not using an email template. You just need to send a single email to multiple users. So there is no need of specifying setTargetObjectId(ID). This is an optional which need to be used if using a template. So remove both below lines and retry it.

  • According to the docs, it's actually optional to include regardless of whether or not there is a template.
    – greenstork
    Sep 11 '14 at 17:51

The problem was that 'body' was null in the cases I was testing. While I'm not sure why Salesforce gives the error it does for this problem, adding a statement along the lines of

if(body == null) body = 'UNKNOWN ERROR';

prior to setting the body of the e-mail solves the issue. Thank you everyone for your help. There are evidently a number of other problems unrelated to the TargetObjectId which can also cause this error, including having an invalid e-mail address in the To or CC fields. Thanks to @greenstork for that one.


The following works for me in anonymous apex; note the setSaveAsActivity. I agree with @highfive that setTargetObjectId() only has utility in supporting merge fields within a template and hence for straight plain text email, has no relevance.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String> {'crop1645@fubar.com'});
email.setCcAddresses(new List<String>{});
email.setbccAddresses(new List<String> {});
email.setSubject('some subject');
email.setPlainTextBody('some body');
email.setSaveAsActivity(false);  // required if sending to a user

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.