First and foremost you need to set the Security Group ID (SGID)
APEX_UTIL.SET_SECURITY_GROUP_ID(APEX_UTIL.FIND_SECURITY_GROUP_ID('MY_WORKSPACE')); -- replace MY_WORKSPACE with yours
then you might need to set an application_id for certain features to work:
where application_name = 'my_app_name'; -- replace my_app_name with yours
If you need to create a session id, you can get a fresh one with
VAL := APEX_CUSTOM_AUTH.GET_NEXT_SESSION_ID;
I build stored packages to call from apex pages. I have my preferred tool (e.g. Toad) to build these packages; I feel comfortable with this tool for DDL and DML. In order to execute the packages I use SQL Workshop because it has an apex context. When I can set an apex context in Toad, I don't need to switch between tools to develop.
>> Another need to establish an apex session outside of an APEX application session is when submitting a scheduled job and wanting that scheduled job to send email using apex_mail.send.
Sorry, but I don't believe that's true. You would need to set the security group ID so that you could use APEX_MAIL.SEND, but you would not need to synthesize a new APEX session to accomplish this.
Indeed setting the security_group_id is enough for sending email through apex_mail.send.
BEGIN apex_util.set_security_group_id(apex_util.find_security_group_id('MY_WORKSPACE')); -- replace MY_WORKSPACE with yours apex_mail.send(p_to => 'email@example.com' ,p_from => 'firstname.lastname@example.org' ,p_body => 'this is body text' ,p_body_html => 'this is body html' ,p_subj => 'this is a subject' ); END; /
That's enough if you don't need to retrieve the text of some messages via apex_lang.message as it is often required for multilingual Apex applications (or simply for application where you don't like to hardcode text message inside procedures), in that case you also need to set the application id as I did in my first reply, otherwise apex_lang.message won't return the message text.