0

I have an issue where I can able to send and email via the 'execute anonymous apex' with no issues, but when I am doing this from my code I cannot get it to send. Also using System.debug I can see that the program is entering my email class and when I query the mail object it is instantiated and populated with all the correct values but just wont seem to send?

So I can send this from the 'execute anonymous apex' - the email sends no problem

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'davidleckenby@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('davidleckenby@gmail.com');  
mail.setSenderDisplayName('Otters Pocket');
mail.setSubject('Test Subject');
mail.setPlainTextBody('Test Subject');
System.debug('mail1' + mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

And this is the class that I created that I mentioned is being reached correctly, accepts the string parameter, instantiates the single email message, and even populates this with the values that I use here. However I cannot seem to get the email to send?

public class emailClass {
public void emailSend(String message) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'davidleckenby@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('davidleckenby@gmail.com');  
mail.setSenderDisplayName('Otters Pocket');
mail.setSubject('Test Subject');
mail.setPlainTextBody('Test Subject');
System.debug('mail1' + mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

}

Also just to add I am calling this email class from an asynchronous future method.

0

Salesforce Spring '13 introduced a new email deliverability setting (Setup > Email Administration > Deliverability). New sandboxes I believe are automatically set to "System email only," causing this error.

If this doesn't work, try calling the code synchronously.

  • Thanks Ohana - but it's set to 'all mail' – David Leckenby May 14 '19 at 1:08
  • @DavidLeckenby I tried running your code when it's called from a trigger. Can you update your class is defined without sharing? so public class without sharing emailClass{...} – PartOfTheOhana May 14 '19 at 1:24
  • No joy with that either – David Leckenby May 14 '19 at 1:27
  • 1
    Hmm... so here is what I would try...try moving it to a non asynchronous method and see if you can send it. If Also, in the future context what user is executing the future, Is it something weird like a site user? What do the debug logs say? Hope this helps! – PartOfTheOhana May 14 '19 at 2:38
  • thank you Ohana - that worked calling from a synchronous class/trigger. – David Leckenby May 14 '19 at 3:51

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.