function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Email.SetOrgWideEmailAddressID timeout issue

I am trying to send out a list of emails using an Org Wide Email address via apex but it is timing out. The emails are sent out quickly without any issues when I don't use the setOrgWideEmailAddressId() function. This is an unacceptable user experience because the page keeps on loading until it times out. 

Unfortunately I need to use an org wide email address because the email must be sent from a generic email, not the logged in user's.


To test out the functionality on its own I executed the following code snippet as annonymous apex and it took 3.2 minutes to finish executing. This is just for a single email.


List<Messaging.Email> emails = new List<Messaging.Email>();
EmailTemplate template = [SELECT Name, Markup, HtmlValue, Body, DeveloperName FROM EmailTemplate WHERE DeveloperName = 'Some_Email_Template'];
OrgWideEmailAddress replyEmail = [SELECT ID, DisplayName FROM OrgWideEmailAddress WHERE Address =: ''];

// The logic here is repeated in a for loop for various contacts
Messaging.SingleEmailMessage currEmail =  new Messaging.SingleEmailMessage();

currEmail.setTargetObjectId('003S000000QcNfl'); // Add a contact ID
currEmail.setWhatId('701S00000007YTgIAM'); // Set a campaign ID

// This is why I am putting the emails in a list, then sending them all


 Last execution at 11/30 13:38:44 in 192,171 milliseconds.



Is there a reason why this takes so long to complete when running as an annonymous apex script? In the UI it times out.



Is there another way I can achieve this without providing an unacceptable user experience?





 I think the mail id “ “is not verified. So please verify that mail by just login with this mail id and you got a URL on this mail id. You have to simply click that URL. You can try your Gmail id to try this one.  Or you may not assign this org wise email address to the desire profile by selecting below 


 Allow All Profiles to Use this From Address


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 



I changed the email address to be generic for the purposes of this forum discussion. The one I am using is already verified in the system, otherwise I wouldn't be able to send out an email because Salesforce would throw an exception stating precisely that the email is not verified.