TrySince we all know that the apex code runs in the current user's context.
So when you try to run the Send Email code from the anonymous window, then the code runs from the System Admin Profile. So here, you will receive the email from the email address assigned to that System Admin User. Since these steps:users have a valid email address, so you are able to receive mail from that user's email address.
But, when the Send Email code triggers from the Guest User's context, then you will not be able to receive email because the guest users are automatically generated when creating a site/community in salesforce and these users have auto-generated invalid email addresses.
So, our goal is to provide a valid email address while sending emails from Guest User's context.
There are 2 approaches to do this.
Approach 1:- Change the Guest User's Email to a valid email address.
Create an orgSince you cannot find the guest user just by navigating Setup -wide-> Users, so you need to go to the Developer Console --> Query Editor and run the query as follows:-
Select Id, FirstName, LastName from User Where UserType = 'Guest'
Copy the Id of your guest user and open it into your org.
Then, Edit the user, and provide any valid email address you like for that guest user.
For more help, you can refer to the following link:-
https://help.salesforce.com/s/articleView?id=000390039&type=1
The problem with this method is that it will work only for that particular guest user's context for which we have provided the valid email address.
Suppose, your org has a large amount of guest users then to make it work for each and every guest user's context, we have to set the valid email address for all the guest users which I believe is a time-consuming and inefficient process. for this, we will use our second approach.
Approach 2:- Using Org-Wide Email Address.
Instead of defining the Valid Email addresses for all the guest users, why don't we define the email address at one place and use for all the guest user right? For this, we will use Org-Wide Email Address
To learn more about the Org-Wide Email Address please refer to the link:- https://documentation.conga.com/clm/latest/setting-up-organization-wide-email-address-152570545.html
Follow these steps:-
Go to Setup --> Organization-Wide Addresses and add theany email address you like and verify it.
In the apex code, add the following:-
List<OrgWideEmailAddress> lstEmailAddress=[select Id from OrgWideEmailAddress WHERE Address=['your org wide email address']]; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTemplateId(et.Id); mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id); mail.setToAddresses(new List<String>{'receiever email address'}); mail.setTargetObjectId(UserInfo.getUserId()); mail.setWhatId(objRegistration.Id); mail.setSaveAsActivity(false); mail.setUseSignature(false); List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>(); allmsg.add(mail); Messaging.sendEmail(allmsg,false);