3

I need to be able to change thefFrom email address when I send an email from Apex code. I can do that using Org-Wide Email addresses, but the issue is that we don't want the user to use that email address when sending emails from the standard UI.

Is there any way to send an email from Apex using an org-wide email address that the profile is not allowed to send from?

  • a simple Custom Setting maybe? with email.setInReplyTo(MyEmailSetting.getInstance()().Org_Email_Address__c) – Cloud Ninja Dec 9 '13 at 0:22
2

Although I have not personally tested this, it would stand to reason that you should be able send the email as any address while within a trigger or a class using "without sharing".

  • 2
    Unfortunately, my class is marked "without sharing" and I still get the error message: "Not profiled to access this Org-wide Email Address" – Daniel Hoechst Dec 6 '13 at 23:08
2

The best idea I have is pretty abstract: you can create a custom object to hold a serialized form of your email that a user has requested to send from a system-managed OrgWideEmailAddress and have a scheduled job running as an admin or otherwise privileged user that actually sends out the email, removing the message from the holding table in the process.

Quite a bit of work to build just for this, but if it's a firm requirement I don't see much in the way of better options.

  • Yeah, that's a lot of work. I think it is more of a nice to have. Thanks for the thoughts, though. – Daniel Hoechst Dec 8 '13 at 0:11
  • That's a pretty good idea, I may try that. Alternatively, you can offload all the email-generating work into the batch job and not serialize anything. Your approach is more generic though, and can be re-used for multiple situations. – Kirill Yunussov May 25 '17 at 20:53
1

You have to Query first like this

OrgWideEmailAddress owea = new OrgWideEmailAddress();
                owea = [SELECT Id, Address, DisplayName FROM 
OrgWideEmailAddress WHERE DisplayName='noreply@domain.com'];

and

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

if ( owea != null) {
mail.setOrgWideEmailAddressId(owea);
}

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.