Está en la página 1de 16

Send Email

From
Oracle Forms Builder

You may download this lesson plan file from my Facebook


group.

https://facebook.com/groups/300518527009351/

If you don't see the file there, type " Send Email From Oracle
Forms Builder " in my Facebook group's search box.
First and Foremost
Please! don’t forget to click or watch at least one Ad that show
up on YouTube during this tutorial.

Thank You! 
Step 1 Signing Up for a New SMTP2GO Account (It’s free )

Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (email) transmission. SMTP2GO.COM provides free outgoing mail SMTP server
If you don't have one.
https://www.smtp2go.com/
Step 2 Creating A New Table and Adding Some Email Addresses

I’m creating a table from rows in Employees table by applying the AS subquery clause, which both creates the table and inserts rows returned from the
subquery.

CREATE TABLE copy_emp


AS
SELECT *
FROM employees;

Update COPY_EMP
SET EMAIL = person1@example.com'
WHERE EMPLOYEE_ID = 100;

Update COPY_EMP
SET EMAIL = 'person2@example.com'
WHERE EMPLOYEE_ID = 101;

Update COPY_EMP
SET EMAIL = 'person3@example.com'
WHERE EMPLOYEE_ID = 102;

Update COPY_EMP
SET EMAIL = 'person4@example.com'
WHERE EMPLOYEE_ID = 103;

Update COPY_EMP
SET EMAIL = 'person5@example.com'
WHERE EMPLOYEE_ID = 104;
Step 3 Creating a New Form

Using the Property Palette, set the following properties on the new item:

Property Value

Name Send_Email
Title My Send Email App

Step 4 Building a Control Block

Using the Property Palette, set the following properties on the new item:

Property Value

Name Send_Email
Single Record Yes
Database Data Block No
Step 5 Adding Non Base Table Items to a Control Block

Add the following items to your form and set their properties as listed:
Property Item Type Name Prompt (Label) Data Type, Database Other Properties Initial Value
Length Item
Text Item TXT_SENDER_NAME Sender Full Name No HR Department

Text Item TXT_SENDER_EMAIL Sender Email Address 64 No The Email address you
specified in your smtp2go
registration form
Text Item TXT_TO To 256 No

Push Button BTN_EMAILS_LIST_TO Show No

Text Item TXT_CC CC 256 No

Push Button BTN_EMAILS_LIST_CC Show No

Text Item TXT_BCC BCC 256 No

Push Button BTN_EMAILS_LIST_BCC Show No

Text Item TXT_SUBJECT Subject No Salary Raise Notification

Value Text Item TXT_MESSAGE_BODY Message Body 2048 No Multiline= Yes

Check Box CHK_FORMAT HTML 5 No Value when Checked = HTML Plain


Value when Unchecked = Plain
Text Item TXT_SMTP_SERVER Outgoing Mail Server (SMTP) No mail.smtp2go.com

Text Item TXT_SMTP_PORT Port Number, 5 No 2525

Text Item TXT_SMTP_USER_NAME User Name No Your smtp2go.com


Username
Text Item TXT_SMTP_PASSWORD Password No Conceal Data = Yes Your smtp2go.com
account password
Push Button BTN_SEND_MESSAGE Send No

Display Item TXT_SELECTED_RECIPIENT N/A 64 No Visible = No

Image Item IMG_STATUS_INDICATOR Status No Image Format = GIF


Sizing Style = Adjust
Width = 25
Height = 75
Step 6 Creating a LOV for displaying and selecting recipient(s) from the table’s email field

In LOV wizard:
 enter the following SQL query statement for the LOV’s Record Group.
SELECT ALL COPY_EMP.FIRST_NAME, COPY_EMP.LAST_NAME, COPY_EMP.EMAIL
FROM COPY_EMP
ORDER BY COPY_EMP.EMPLOYEE_ID ASC

 enter a Return value for EMAIL column of our LOV as SEND_MAIL.TXT_SELECTED_RECIPIENT

Using the Property Palette, set the following properties on the new LOV:

Property Value

Name LOV_EMAILS_LIST
Title Emails List
Width 508
Height 300

Also change LOV’s Record Group name to RG_EMAILS_LIST.


Step 7 Adding Functionality with Code

In this step, we are adding functionality by coding PL/SQL program units and triggers in our form. We shall create the following procedures or functions in our
form.
FUNCTION CREATE_RECIPIENTS_LIST (recipients IN VARCHAR2) RETURN VARCHAR2 IS
a_value_chosen BOOLEAN;
recipientsList VARCHAR2(256) := recipients;
BEGIN
a_value_chosen := Show_Lov('LOV_EMAILS_LIST');

IF NOT a_value_chosen THEN


Message('You have not selected a value.');
Bell;
RAISE Form_Trigger_Failure;
ELSIF recipientsList IS NULL THEN
recipientsList := :TXT_SELECTED_RECIPIENT;
ELSE
recipientsList := recipientsList || ',' || :TXT_SELECTED_RECIPIENT;
END IF;

RETURN recipientsList;
END;
PROCEDURE SPLIT_MULTIPLE_EMAIL_ADDRESSES (conexion IN OUT UTL_SMTP.connection, p_list IN VARCHAR2, p_del IN VARCHAR2) IS
l_idx pls_integer;
l_list varchar2(256) := p_list;
l_value varchar2(256);
begin
loop
l_idx := instr(l_list,p_del);
if l_idx > 0 then
UTL_SMTP.rcpt(conexion, substr(l_list,1,l_idx-1));
l_list := substr(l_list,l_idx+length(p_del));

else
UTL_SMTP.rcpt(conexion, l_list);
exit;
end if;
end loop;
END SPLIT_MULTIPLE_EMAIL_ADDRESSES;
PROCEDURE SET_STATUS_INDICATOR (indicatorColor IN VARCHAR2) IS
gif_image_dir VARCHAR2(80) := 'D:\Send_EMail\';
photo_filename VARCHAR2(80);
BEGIN
/*
** Set the message level high so we can gracefully handle
** an error reading the file if it occurs
*/
:System.Message_Level := '25';
/*
** filename from which to load the GIF image. The form
** has a image item named 'IMG_STATUS_INDICATOR'
** into which we read the image.
*/
photo_filename := gif_image_dir|| 'Trafic Lights ' || indicatorColor ||'.gif';

/*
** For example 'photo_filename' might look like:
**
** D:\Send_EMail\Trafic Lights Green.gif
** ------
**
** Now, read in the appropriate image.
*/

READ_IMAGE_FILE(photo_filename, 'GIF', 'IMG_STATUS_INDICATOR');


IF NOT FORM_SUCCESS THEN
MESSAGE('Missing Status');
END IF;
:SYSTEM.MESSAGE_LEVEL := '0';
END;
PROCEDURE Send_EMail
(
p_Sender_Name IN VARCHAR2, -- Sender name as you would like it to appear in messages that you send.
p_Sender_Email IN VARCHAR2, -- Sender email address for this account, such as appleseed@example.com.

p_To IN VARCHAR2, -- To: primary recipients.


p_CC IN VARCHAR2, -- CC: carbon copy to secondary recipients (other interested parties).
p_BCC IN VARCHAR2, -- BCC: blind carbon copy to tertiary recipients who receive the message. BCC is used to send a
message to multiple parties with none of them knowing the other recipients.

p_Subject IN VARCHAR2,
p_message_Body IN VARCHAR2,

p_SMTP_Server IN VARCHAR2, -- The host name of the outgoing mail server (SMTP), such as smtp.example.com.
p_SMTP_Port IN NUMBER, -- The port number used by the outgoing mail server. Common port numbers for outgoing
mail are 25, 465, and 587.
p_SMTP_Username IN VARCHAR2, -- Your email account user name, such as appleseed. Some email providers want your full email
address as your user name.
p_SMTP_Password IN VARCHAR2, -- The email password you use to sign in to your account.

p_Format IN VARCHAR2
)

IS

v_Mail_Conn UTL_SMTP.connection;
v_Encoded_Username varchar2(2048);
v_Encoded_Password varchar2(2048);
CRLF VARCHAR2(2) := chr(13)||chr(10);/*

chr(13) = Carriage Return (moves cursor to leftmost side)

chr(10) = New Line (drops cursor down one line)


So
the combination of chr(13) and chr(10) is to create a line break within a string.
In
short it's just like 'dbms_output.put_line'.
*/
BEGIN
--open connection
v_Encoded_Username := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(p_SMTP_Username)));
v_Encoded_Password := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(p_SMTP_Password)));
v_Mail_Conn := UTL_SMTP.open_connection(p_SMTP_Server, p_SMTP_Port);
UTL_SMTP.ehlo(v_Mail_Conn, p_SMTP_Server);
UTL_SMTP.command(v_Mail_Conn, 'AUTH', 'LOGIN');
UTL_SMTP.command(v_Mail_Conn, v_Encoded_Username);
UTL_SMTP.command(v_Mail_Conn, v_Encoded_Password);

--prepare headers
UTL_SMTP.mail(v_Mail_Conn, p_Sender_Email);

/*if we have multiple recipients or CCs, we must call UTL_SMTP.rcpt once for each one
however, we shall specify that there are CCs in the mail header in order for them to appear as such*/
SPLIT_MULTIPLE_EMAIL_ADDRESSES(v_Mail_Conn, p_To, ',');
IF TRIM(p_CC) IS NOT NULL THEN
SPLIT_MULTIPLE_EMAIL_ADDRESSES(v_Mail_Conn, p_CC, ',');
END IF;
IF TRIM(p_BCC) IS NOT NULL THEN
SPLIT_MULTIPLE_EMAIL_ADDRESSES(v_Mail_Conn, p_BCC, ',');
END IF;

UTL_SMTP.open_data(v_Mail_Conn);

--prepare mail header


/*DO NOT USE MON instead of MM in the date pattern if you run the script on machines with different locales as it will be misunderstood
and the mail date will appear as 01/01/1970*/
UTL_SMTP.write_data(v_Mail_Conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || CRLF);
UTL_SMTP.write_data(v_Mail_Conn, 'To: ' || REPLACE(p_To, ',', ';') || CRLF);
IF TRIM(p_CC) IS NOT NULL THEN
UTL_SMTP.write_data(v_Mail_Conn, 'CC: ' || REPLACE(p_CC, ',', ';') || CRLF);
END IF;
/*
IF TRIM(p_BCC) IS NOT NULL THEN
UTL_SMTP.write_data(v_Mail_Conn, 'BCC: ' || REPLACE(p_BCC, ',', ';') || CRLF);
END IF;
*/
UTL_SMTP.write_data(v_Mail_Conn, 'From: ' || p_Sender_Name || ' <' || p_Sender_Email || '>' || CRLF);
UTL_SMTP.write_data(v_Mail_Conn, 'Subject: ' || p_Subject || CRLF);
UTL_SMTP.write_data(v_Mail_Conn, 'Reply-To: ' || p_Sender_Email || CRLF);

--include the message body


IF p_Format = 'HTML' THEN
UTL_SMTP.write_data(v_Mail_Conn, 'MIME-Version: 1.0' || CRLF || 'Content-type: text/html' || CRLF || p_message_Body );
ELSE
UTL_SMTP.write_data(v_Mail_Conn, p_message_Body);
END IF;

--send the email


UTL_SMTP.close_data(v_Mail_Conn);
UTL_SMTP.quit(v_Mail_Conn);

SET_STATUS_INDICATOR('Green');

EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
UTL_SMTP.QUIT(v_Mail_Conn);
EXCEPTION
WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN
NULL; -- When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The QUIT call raises an
-- exception that we can ignore.
END;
Message(sqlerrm);
SET_STATUS_INDICATOR('Red');
END;
BTN_EMAILS_LIST_TO
BEGIN
:TXT_TO := CREATE_RECIPIENTS_LIST(:TXT_TO);
END;

BTN_EMAILS_LIST_CC
BEGIN
:TXT_CC := CREATE_RECIPIENTS_LIST(:TXT_CC);
END;

BTN_EMAILS_LIST_BCC
BEGIN
:TXT_BCC := CREATE_RECIPIENTS_LIST(:TXT_BCC);
END;

BTN_SEND_MESSAGE
BEGIN

SEND_EMAIL (:TXT_SENDER_NAME, :TXT_SENDER_EMAIL, :TXT_TO, :TXT_CC, :TXT_BCC, :TXT_SUBJECT, :TXT_MESSAGE_BODY, :TXT_SMTP_SERVER,


:TXT_SMTP_PORT, :TXT_SMTP_USER_NAME, :TXT_SMTP_PASSWORD, :CHK_FORMAT);

EXCEPTION WHEN OTHERS THEN

SET_STATUS_INDICATOR('Red');

END;
Step 8 Creating an Access Control List (ACL)
Oracle 11g introduces fine grained access to network services using access control lists (ACL) in the XML DB repository, allowing control over which users access
which network resources, regardless of package grants.

BEGIN
DBMS_NETWORK_ACL_ADMIN.create_acl (
acl => 'Send_Email.xml',
description => 'Allow mail to be send.',
principal => 'HR',
is_grant => TRUE,
privilege => 'connect',
start_date => SYSTIMESTAMP,
end_date => NULL);
COMMIT;
END;

Assign an ACL to a Network


BEGIN
DBMS_NETWORK_ACL_ADMIN.assign_acl (
acl => 'Send_Email.xml',
host => 'mail.smtp2go.com',
lower_port => 2525,
upper_port => NULL);
COMMIT;
END;

OPTIONAL
How to view ACL.
SELECT *
FROM dba_network_acls;

How to delete ACL.


begin
dbms_network_acl_admin.drop_acl(
'Send_Email.xml'
);
end;
Step 9 Running our App
Our “Send Email from Oracle Forms Builder” app is complete. We can now run it.

Sending a plain text message


Dear Employees:

I am pleased to inform you that your salary is being increased as of January 1, 2016.

You are being awarded a 3% merit increase due to your excellent performance and valuable contributions to your department and to the corporation. Your
new annual salary will be $120,000.

In addition to your salary increase, many of your benefits such as retirement are indexed to your salary and will be increased accordingly.

Thank you for your contributions to the company.

Sincerely,
Human Resources Department

Sending a HTML message


Hello Viewers
Everyone knows that YouTube Video Producers make their money off of advertisers. Ads are paid Pay-per-click or Pay-per-view. If people skip an Ad on
YouTube, they are denying the content creator valuable ad revenue.

People should stop skipping ads when they watch some of their favorite creators... don't make them to go out of business! Also the best thing you can do for
youTubers is sharing their content.

Muhammad Abid
YouTube Video Producer

http://www.sautinsoft.net/

También podría gustarte