To Check if Org-wide email is verified in APEX

I don't think it is accessible by any API, although you can consider the following workaround: you could store all the verified OrgWideEmailAddress in some custom setting or label to use it as a cache and populate your picklist from there. Then, when something new appears in the OrgWideEmailAddress collection, try to send a test email only for the new one (that is not present in your cache collection) like this (not fully tested so take it as pseudo code):

for (OrgWideEmailAddress addr : [SELECT id, DisplayName, Address 
        FROM OrgWideEmailAddress where Address NOT IN : myCachedCollection]) {

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setOrgWideEmailAddressId(addr.id);
    mail.setPlainTextBody('test');
    mail.setToAddresses(new List<string> { '[email protected]' });

    try {
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }catch(EmailException e){
        // IF MESSAGE CONTAINS "UNVERIFIED_SENDER_ADDRESS":
            // UPS! NOT VERIFIED YET
            continue;
    }

    //ADD EMAIL TO CACHE HERE
}

If it is verified, add it to your cached OrgWideEmailAddress collcetion.

If it is not verified, the System.EmailException with UNVERIFIED_SENDER_ADDRESS message will be thrown, letting you know that it shouldnt be used yet.

You can also handle removing OrgWideEmailAddress with some trigger or even up here. Good luck!


Bart's solution is a well-crafted one and probably the best for your situation. I had a similar need to query only verified addresses but my criteria were a bit more flexible so I was able to take a simpler approach. I catch the exception and if it was due to an unverified address I resend with a default company OrgWideEmailAddress that we use for more generic operations. Obviously a less personal approach but at least this way the email goes out. Remember that if using sendEmail() to send to a list you can set the optional all-or-none flag to false (default is true) to achieve partial success. You can then analyze the returned email results to handle any individual exceptions.

Tags:

Email

Apex