We have one visualforce page which is working fine previously for sending emails but now it is not working. All other functions from that visualforce page are working fine But send mail functionality is not working which worked previously. For the admin user, it is working fine if we preview the visualforce page from the developer console. It is not working only for guest users. Also checked the profile and site setting but did not find anything.

For sending mail code is given below:-

Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();
List<String> sendTo = new List<String>();
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName('twopir consulting');
mail.setSubject('Token for Employee Portal Login');
String body = '<html><body>Dear ' + contactList[0].Name + ',<br><br>Use Token : ' +systemToken+' to log into Employee Portal</body></html>';

Can you please help us for solving the issue?

  • what does "not working" mean?, Messaging.sendEmail returns Messaging.SendEmailResult which could be inspected
    – cropredy
    Jul 14, 2022 at 17:17
  • @cropredy Not working means the mail is not getting sent to the user. And in debug, SendEmailResult method isSuccess returns true
    – Darshana
    Jul 15, 2022 at 14:54

3 Answers 3


Make sure your Guest User has a valid email assigned.

This is discussed in the article listed here.

  • Yes, we have assigned a valid email address to the guest user.
    – Darshana
    Jun 29, 2022 at 4:33
  • 1
    To anyone looking around for a solution, this seems to be the key step to take to make this work. I did not have to have "Send Email" on the profile. Setting an Org Wide Email address did not work.
    – Keith C
    Feb 28, 2023 at 12:29
  • Nice... It seems particularly relevant for sandboxes where user email addresses are all invalid by default Jul 31, 2023 at 7:55

Do the users have the relevant Apex class in their profile or PermissionSet? Do you get an error message logged?

  • Yes, we have assigned apex classes and all permissions. No, we don't get any error messages. Only the mail is not sent to users.
    – Darshana
    Jul 15, 2022 at 12:23

You can set organization-Wide Addresses and use them when you want to send the email.

Docuement: https://help.salesforce.com/s/articleView?id=sf.email_default_no_reply.htm&type=5

Example: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setOrgWideEmailAddressId

Please try as suggested, it should work

You must log in to answer this question.

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