I have a visualforce page which does some calculations and sends email to the concerned people.

The problem I am facing is - the email is sent from the apex class perfectly without any errors or limits exceeded but it is not received at the users end.

I checked the spams for the email accounts and the email was not there.

I also do not see any errors happening at apex side. To test it further, I removed the email functionality from the logic and just did a dummy email test run, but it did not work either

private static void testEmail() {
    try {
        List<String> emailAddressList = new List<String>();
        emailAddressList.add('someemail@email.com'); -- (I put my personal email here)
        List<Messaging.SingleEmailMessage> mailsList = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('subject for this email');
        mail.setHtmlBody('blab blah blah.....');
        List<Messaging.SendEmailResult> emailResultList = Messaging.sendEmail(mailsList);
        System.debug('-------------------------------------- Email sent status : ' + emailResultList[0].isSuccess());
    } catch(Exception e) {
        System.debug('---------------- Error occurred in testEmail() : ' + e.getStackTraceString());

When this method is called, the email sent status is true.

debug screenshot

This is a simple email and it did not work. I don't understand what is going wrong.

  • Can you post your visualforce page – sfdc Sep 2 '14 at 19:43
  • It an empty page with a controller (I have not written any vf code yet). The constructor calls this testEmail method. – codeinprogress Sep 2 '14 at 19:46
  • does ur vf page has apex:form tags in it – sfdc Sep 2 '14 at 20:22
  • No the page is empty, there is nothing in there. I just refresh the page and the constructor calls that method. – codeinprogress Sep 2 '14 at 20:35
  • 1
    Apex Message.sendEmail must be only used in an action with a form submit. It will not work with an AJAX call. – sfdc Sep 2 '14 at 20:57

Found this: https://help.salesforce.com/apex/HTViewSolution?id=000171487&language=en_US

Looks like you can't send email from a constructor directly. In that knowledge article is says if you want to do something on page load to put it in a method in a controller and set it to the 'action=""' on your vf page tag

  • Wow, did not knew that. Thanks for the answer. – codeinprogress Sep 3 '14 at 14:04

It may be too simple but ensure that you have email delivery turned on for your org:

1: go to the setup tab 2: click on Email Administration 3: click on Deliverability 4: ensure the "Access to Send Email" section has "All Emails" selected in the Access Level dropdown list. enter image description here

  • It was already set to All email. The weird thing is, I have another email functionality which works perfectly fine. But this little method is not working. – codeinprogress Sep 2 '14 at 20:00
  • 1
    Thanks, I see you found an answer though (Or it appears as such). I'm not sure I follow though, the solution mentions not being able to send it from a constructor, but your code doesn't look like it's in a constructor...I don't have enough rep to comment on the solution though :( – lifewithryan Sep 3 '14 at 20:32
  • 1
    Even though my code was not in constructor it was called from a constructor. The testEmail method was called from constructor. So as it turns out, email cannot be sent from a constructor (which makes sense given how many times a page can be refreshed). More helpful link - help.salesforce.com/apex/… – codeinprogress Sep 3 '14 at 21:25

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.