Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending UTL_SMTP mail with jpg picture inside an e-mail

Siva.PMar 5 2014 — edited Mar 6 2014

Hi -

I have to embed a picture in a e-mail for my work, Tested with different options/mime types but no luck.

When I tried with the below code the JPG file is e-mailed as an attachment with an e-mail content. I dont want jpg as attachment, it should show inside the e-mail.

Any help on this is appreciated.

Here is my code I tested and jpg file working as an attachment.

DECLARE
conn UTL_SMTP.CONNECTION;
p_from_address VARCHAR2(100) := 'from@abc.com';
p_to_address VARCHAR2(100) := 'to@abc.com';
p_subject VARCHAR2(100)  := 'Test Mail Subject';
p_message VARCHAR2(1000) := 'Test Mail Body';
MailServer  VARCHAR2(30) := 'xxx.xxx.xxx'; -- Can be changed to any smtp server
BOUNDARY  VARCHAR2 (256) := '-----090303020209010600070908';

      fn_header   VARCHAR2 ( 16 ) DEFAULT 'banner.jpg'; 
      ft_header   UTL_FILE.file_type;
       DATA        RAW ( 5000 );
       max_base64_line_width CONSTANT PLS_INTEGER := 76 / 4 * 3; 
      
BEGIN
conn := utl_smtp.open_connection(MailServer, 25);
utl_smtp.helo(conn, MailServer);
utl_smtp.mail(conn, p_from_address);
utl_smtp.rcpt(conn, p_to_address); -- Initial Operations.
utl_smtp.open_data(conn);
utl_smtp.write_data(conn,'From: ' || p_from_address || UTL_TCP.CRLF);
utl_smtp.write_data(conn,'To: ' || p_to_address || UTL_TCP.CRLF);

UTL_SMTP.write_data (conn, 'MIME-Version: 1.0' || CHR (13) || CHR (10));
--UTL_SMTP.write_data (conn, 'Content-Transfer-Encoding: 8bit' || CHR (13) || CHR (10));
utl_smtp.write_data (conn,'Subject:' || p_subject || UTL_TCP.CRLF);
UTL_SMTP.write_data (conn, 'Content-Type: multipart/mixed; boundary="' || BOUNDARY || '"' || UTL_TCP.CRLF);
--UTL_SMTP.write_data (conn, 'X-Mailer:Mailer by Oracle UTL_SMTP');
UTL_SMTP.write_data (conn, UTL_TCP.CRLF);
UTL_SMTP.write_data (conn,  '--' || BOUNDARY || UTL_TCP.CRLF );
UTL_SMTP.write_data (conn,  'Content-Type: text/html; charset=US-ASCII'|| UTL_TCP.CRLF );
UTL_SMTP.write_data (conn, UTL_TCP.CRLF);
UTL_SMTP.write_data (conn, 'HTML Test Here');
UTL_SMTP.write_data (conn, UTL_TCP.CRLF);

UTL_SMTP.write_data (conn,  '--' || BOUNDARY || UTL_TCP.CRLF );

ft_header := UTL_FILE.fopen ('RMS_FTP_DIR', fn_header, 'RB', 32767);

utl_smtp.write_data(conn,'Content-Type: image/jpg'|| UTL_TCP.CRLF);
utl_smtp.write_data(conn,'Content-Disposition: inline; filename=banner.jpg'|| UTL_TCP.CRLF);
utl_smtp.write_data(conn,'Content-Transfer-Encoding: base64'|| UTL_TCP.CRLF);
UTL_SMTP.write_data (conn, UTL_TCP.crlf );
UTL_SMTP.write_data (conn, UTL_TCP.crlf );

BEGIN
              LOOP
                 UTL_FILE.get_raw(ft_header, DATA, max_base64_line_width);
                -- write_raw (conn      => conn,
                  --           MESSAGE   => UTL_ENCODE.base64_encode (DATA));
                 UTL_SMTP.write_raw_data (conn, UTL_ENCODE.base64_encode (DATA) );           
              END LOOP;
           EXCEPTION
              WHEN NO_DATA_FOUND
              THEN
                 UTL_FILE.fclose (ft_header);
END;                       

UTL_SMTP.write_data (conn, UTL_TCP.crlf );

IF UTL_FILE.is_open (ft_header)
THEN
    UTL_FILE.fclose (ft_header);
END IF;

UTL_SMTP.write_data (conn,  '--' || BOUNDARY || '--' || UTL_TCP.CRLF );

utl_smtp.close_data(conn);
utl_smtp.quit(conn);
DBMS_OUTPUT.PUT_LINE('GOOD' ) ;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Error Sending Mail' ) ;
        DBMS_OUTPUT.PUT_LINE('Error Message ' || SQLERRM) ;
       DBMS_OUTPUT.PUT_LINE('BAD' ) ;
END;

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2014
Added on Mar 5 2014
6 comments
3,998 views