Utl_smtp send_header ('To'

Hi,
using utl_smtp I want to send the mail. I look for the syntaxe of :
send_header('To', '"Recipient" <[email protected]>');
send_header('cc:', '"Recipient" <[email protected]>');
When you have multipel recipient. For exemple :
send_header('To', '"Recipient" <[email protected]>, <[email protected]>', <[email protected]>'');Or
send_header('cc', '"Recipient" <[email protected]>, <[email protected]>', <[email protected]>'');
I looked here :
http://www.psoug.org/reference/utl_smtp.html
But no exemple. Would you please give me the correct syntax?
Many thanks.

Hi,
May the following link help you [Invalid mail id|http://www.dba-oracle.com/t_utl_smtp_utility.htm|click]
*009*

Similar Messages

  • Extending send_mail

    I am using this code (below) within a procedure and it is working great. But, I can not figure out the symantecs on how to add more than the two e-mails to the recipient list. Any help would be greatly appreciated.
    Thanks.
    Brian
    send_email( sender => '[email protected]',
    recipient => '[email protected]',
    add_recip => '[email protected]',
    subject => 'test',
    message => .....

    I found another alternative that you can try which purportedly allows you to use multiple recipients. This one I got from Metalink. I haven't tried it myself, but I assume it works. Here it is pasted below:
    From: Michael Devlin 11-Sep-02 13:12
    Subject: Re : utl_smtp for multiple recipients
    In case anyone is looking for the code to actually do this:
    (sender varchar2, recipient varchar2, subj varchar2, msg varchar2) is
    --(sender varchar2, recipient varchar2, subrecipient varchar2, subj varchar2, msg varchar2) is
    --v_OutPutFileHandle UTL_FILE.FILE_TYPE ;
    conn utl_smtp.connection;
    smtp_host varchar2(50) := 'fs-ajd03';
    port varchar2(2) := 25;
    --mesg varchar2(2000) := null;
    cr varchar2(2) := chr(10)||chr(13);
    subj_line varchar2(50);
    commaPos number(3) := 0;
    recipientlist varchar2(2000);
    subrecipient varchar2(50) := '';
    BEGIN
    --v_OutputFileHandle := UTL_FILE.FOPEN('c:\temp\', 'test1.txt', 'w');
    conn := utl_smtp.open_connection(smtp_host, port);
    utl_smtp.helo(conn, smtp_host);
    utl_smtp.mail(conn, sender);
    --UTL_FILE.PUT_LINE(v_OutputFileHandle, sender);
    UTL_FILE.PUT_LINE(v_OutputFileHandle, '---------------');
    --UTL_FILE.PUT_LINE(v_OutputFileHandle, recipient);
    commaPos := INSTR(recipient, ',');
    IF commaPos > 0 THEN
    recipientlist := recipient;
    LOOP
    subrecipient := SUBSTR(recipientlist, 1, INSTR(recipientlist, ',', 1, 1) - 1);
    --UTL_FILE.PUT_LINE(v_OutputFileHandle, subrecipient);
    utl_smtp.rcpt(conn, subrecipient);
    commaPos := INSTR(recipientlist, ',');
    recipientlist := LTRIM(SUBSTR(recipientlist, commaPos + 1));
    --UTL_FILE.PUT_LINE(v_OutputFileHandle, recipientlist);
    commaPos := INSTR(recipientlist, ',');
    IF commaPos = 0 THEN
    utl_smtp.rcpt(conn, recipientlist);
    EXIT;
    END IF;
    END LOOP;
    ELSE
    utl_smtp.rcpt(conn, recipient);
    END IF;
    utl_smtp.open_data(conn);
    subj_line := 'Subject: '||subj || cr;
    utl_smtp.write_data(conn, subj_line);
    --utl_smtp.write_data(conn, 'Date: ' || TO_CHAR( SYSDATE, 'dd-Mon-yy hh24:mi:ss' ) || cr);
    --utl_smtp.send_header(mail_conn,' ');
    utl_smtp.write_data(conn, msg);
    utl_smtp.close_data(conn);
    utl_smtp.quit(conn);
    --UTL_FILE.FCLOSE(v_OutputFileHandle);
    /*EXCEPTION
    WHEN UTL_FILE.INVALID_PATH THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20100, 'Reset: Invalid Path');
    WHEN UTL_FILE.INVALID_MODE THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Invalid Mode');
    WHEN UTL_FILE.INVALID_OPERATION THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Invalid Operation');
    WHEN UTL_FILE.INVALID_FILEHANDLE THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Invalid File Handle');
    WHEN VALUE_ERROR THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Value Error');
    WHEN UTL_FILE.READ_ERROR THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Read Error');
    WHEN UTL_FILE.INTERNAL_ERROR THEN
    UTL_FILE.FCLOSE(v_OutputFileHandle);
    RAISE_APPLICATION_ERROR(-20101, 'Reset: Internal Error');*/
    END;
    Michael P. Devlin
    Developer/Programmer
    A. James de Bruin & Sons
    [email protected]

  • Problem with utl_smtp.write_data - immediate help needed

    Hi,
    I have a sql code that generates a .csv file and sends it as an attachment through utl_smtp.
    Everything works fine but the .csv file's header gets broken after certain characters..wondering why this is happening..pls help.
    Please find my code below:
    SET define off;
    DECLARE
    --CURSOR VARIABLES
    v_meter_count NUMBER := 0;
    v_meter_max_count NUMBER := 0;
    v_sysdate DATE := TRUNC (SYSDATE);
    --MAIL VARIABLES
    v_run_date DATE := TRUNC (SYSDATE);
    crlf CONSTANT VARCHAR2 (10) := UTL_TCP.crlf;
    boundary CONSTANT VARCHAR2 (256)
    := '-----7D81B75CCC90D2974F7A1CBD';
    first_boundary CONSTANT VARCHAR2 (256) := '--' || boundary || crlf;
    last_boundary CONSTANT VARCHAR2 (256)
    := '--' || boundary || '--' || crlf;
    multipart_mime_type CONSTANT VARCHAR2 (256)
    := 'multipart/mixed; boundary="' || boundary || '"';
    conn UTL_SMTP.connection;
    from_name VARCHAR2 (255) := 'Nielsen Online';
    from_address VARCHAR2 (255)
    := '[email protected]';
    to_address VARCHAR2 (255)
    := '[email protected]';
    subject VARCHAR2 (255) := '';
    mime_type VARCHAR2 (255) := 'text/plain';
    attachment_file_name VARCHAR2 (255) := NULL;
    mailhost VARCHAR2 (255) := '127.0.0.1';
    --PANELISTS' DETAILS CURSOR
    CURSOR tam_panelists
    IS
    SELECT a.panel_sys_id, a.panel_site_id, a.login_id,
    b.recruit_start_date, c.meter_id, d.affiliate_id,
    d.afflt_member_id,
    (CASE
    WHEN b.last_active_date >= TRUNC (SYSDATE) - 30
    THEN 'Active'
    ELSE 'Inactive'
    END
    ) hh_status,
    (SELECT e.affiliate_description
    FROM pfshared3.lkp_affiliates e
    WHERE e.partner_id =
    NVL (d.affiliate_id, 0)
    AND e.pnl_sys_id = a.panel_sys_id) affiliate_description
    FROM mgtw.dat_ps_login_info a,
    mgtw.dat_ps_install_info b,
    mgtw.dat_ps_info c,
    mgtw.dat_ps_recruit_info d
    WHERE a.login_id LIKE 'tam%@tam%.com'
    AND a.panel_site_id = b.panel_site_id
    AND a.panel_sys_id = b.panel_sys_id
    AND a.panel_site_id = c.panel_site_id
    AND a.panel_sys_id = c.panel_sys_id
    AND a.panel_site_id = d.panel_site_id
    AND a.panel_sys_id = d.panel_sys_id
    AND a.active_flag = 1
    AND b.first_mtr_install_date IS NOT NULL
    AND b.last_active_date IS NOT NULL;
    --PANELISTS' PC DETAILS CURSOR
    CURSOR pc_activity (p_meter_id IN VARCHAR2)
    IS
    SELECT cpt_guid, first_active_date, last_active_date,
    TO_CHAR(first_install_date,'MM/DD/YYYY HH:MI') first_install_dt,
    (TRUNC (SYSDATE) - last_active_date) inactive_days,
    (CASE
    WHEN last_active_date >= TRUNC (SYSDATE) - 30
    THEN 'Active'
    ELSE 'Inactive'
    END
    ) status
    FROM pfshared3.dat_pnl_computers
    WHERE meter_id = p_meter_id
    ORDER BY last_active_date DESC;
    --RECORD TYPE FOR PANELISTS' DETAILS
    TYPE t_panelist_set IS TABLE OF tam_panelists%ROWTYPE;
    v_panelist_data t_panelist_set;
    --RECORD TYPE FOR PANELISTS' PC DETAILS
    TYPE t_pc_set IS TABLE OF pc_activity%ROWTYPE;
    v_pc_data t_pc_set;
    PROCEDURE send_header (NAME IN VARCHAR2, header IN VARCHAR2)
    IS
    BEGIN
    UTL_SMTP.write_data (conn, NAME || ': ' || header || crlf);
    END;
    BEGIN
    --MAX METER COUNT
    BEGIN
    SELECT MAX(COUNT (meter_id))
    INTO v_meter_max_count
    FROM pfshared3.dat_pnl_computers
    WHERE last_active_date IS NOT NULL
    GROUP BY meter_id;
    EXCEPTION
    WHEN OTHERS
    THEN
    v_meter_max_count := 0;
    END;
    DBMS_OUTPUT.put_line ( 'STARTED - '
    || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
    conn := UTL_SMTP.open_connection (mailhost);
    UTL_SMTP.helo (conn, mailhost);
    UTL_SMTP.mail (conn, '< ' || from_address || ' >');
    UTL_SMTP.rcpt (conn, '< ' || to_address || ' >');
    UTL_SMTP.open_data (conn);
    send_header ('From', '"' || from_name || '" <' || from_address || '>');
    send_header ('To', '' || to_address || '');
    send_header ('Date', TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss'));
    subject := 'TW Daily Activity Report' || ' - ' || v_run_date;
    send_header ('Subject', subject);
    send_header ('Content-Type', multipart_mime_type);
    -- Close header section by a crlf on its own
    UTL_SMTP.write_data (conn, crlf);
    -- mime header
    UTL_SMTP.write_data (conn, first_boundary);
    send_header ('Content-Type', mime_type);
    UTL_SMTP.write_data (conn, crlf);
    UTL_SMTP.write_data (conn, crlf);
    -- add the attachment
    attachment_file_name := 'TW_Daily_Activity_Report_' || v_run_date || '.txt';
    UTL_SMTP.write_data (conn, first_boundary);
    send_header ('Content-Type', mime_type);
    send_header ('Content-Disposition',
    'attachment; filename= ' || attachment_file_name
    UTL_SMTP.write_data (conn, crlf);
    --CREATING HEADERS
    UTL_SMTP.write_data (conn,
    'Date of Reporting'
    || ','
    || 'Panelist ID'
    || ','
    || 'Meter ID'
    || ','
    || 'Panelist Email(Username)'
    || ','
    || 'Vendor'
    || ','
    || 'Affiliate ID'
    || ','
    || 'UID'
    || ','
    || 'Enrollment Date'
    || ','
    || 'HH Status'
    || ','
    || 'Registered PC in the HH'
    || ','
    FOR k IN 1 .. v_meter_max_count
    LOOP
    UTL_SMTP.write_data (conn,
    'Installation Date/Time MM/DD/YYYY HH:MI PC' || k || ',' || 'PC '|| k ||': Status' || ',' || 'PC '|| k ||': Date of First log' || ',' || 'PC '|| k ||': Date of Last log' || ',' || 'PC '|| k ||': Days since Last Activity' || ',' || 'PC '|| k ||': Computer ID' || ','
    END LOOP;
    UTL_SMTP.write_data (conn, crlf);
    --OPENING PANELISTS' DETAILS CURSOR
    OPEN tam_panelists;
    --FETCHING PANELISTS' DETAILS CURSOR
    FETCH tam_panelists
    BULK COLLECT INTO v_panelist_data;
    FOR i IN v_panelist_data.FIRST .. v_panelist_data.LAST
    LOOP
    --METER COUNT
    BEGIN
    SELECT COUNT (meter_id)
    INTO v_meter_count
    FROM pfshared3.dat_pnl_computers
    WHERE meter_id = v_panelist_data (i).meter_id
    GROUP BY meter_id;
    EXCEPTION
    WHEN OTHERS THEN
    v_meter_count := 0;
    END;
    --OPENING PANELISTS' PC DETAILS CURSOR
    OPEN pc_activity (v_panelist_data (i).meter_id);
    --FETCHING PANELISTS' PC DETAILS CURSOR
    FETCH pc_activity
    BULK COLLECT INTO v_pc_data;
    UTL_SMTP.write_data (conn,
    v_sysdate
    || ','
    || v_panelist_data (i).panel_site_id
    || ','
    || v_panelist_data (i).meter_id
    || ','
    || v_panelist_data (i).login_id
    || ','
    || v_panelist_data (i).affiliate_description
    || ','
    || v_panelist_data (i).affiliate_id
    || ','
    || v_panelist_data (i).afflt_member_id
    || ','
    || v_panelist_data (i).recruit_start_date
    || ','
    || v_panelist_data (i).hh_status
    || ','
    || v_meter_count
    || ','
    FOR j IN v_pc_data.FIRST .. v_pc_data.LAST
    LOOP
    UTL_SMTP.write_data (conn,
    v_pc_data (j).first_install_dt
    || ','
    || v_pc_data (j).status
    || ','
    || v_pc_data (j).first_active_date
    || ','
    || v_pc_data (j).last_active_date
    || ','
    || v_pc_data (j).inactive_days
    || ','
    || v_pc_data (j).cpt_guid
    || ','
    END LOOP;
    UTL_SMTP.write_data (conn, crlf);
    CLOSE pc_activity;
    END LOOP;
    CLOSE tam_panelists;
    -- CLOSE THE MESSAGE
    UTL_SMTP.write_data (conn, last_boundary);
    -- CLOSE CONNECTION
    UTL_SMTP.close_data (conn);
    UTL_SMTP.quit (conn);
    DBMS_OUTPUT.put_line ( 'ENDED - '
    || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
    RETURN;
    EXCEPTION
    WHEN UTL_SMTP.transient_error OR UTL_SMTP.permanent_error
    THEN
    UTL_SMTP.quit (conn);
    raise_application_error
    (-20000,
    'Failed to send mail due to the following error: '
    || SQLERRM
    WHEN OTHERS
    THEN
    raise_application_error
    (-20000,
    'Script failed due to the following error: '
    || SQLERRM
    RETURN;
    END;
    Please find below the header of the .csv file generated:
    Date of Reporting     Panelist ID     Meter ID     Panelist Email(Username)     Vendor     Affiliate ID     UID     Enrollment Date     HH Status     Registered PC in the HH     Installation Date/Time MM/DD/YYYY HH:MI PC1     PC 1: Status     PC 1: Date of First log     PC 1: Date of Last log     PC 1: Days since Last Activity     PC 1: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC2     PC 2: Status     PC 2: Date of First log     PC 2: Date of Last log     PC 2: Days since Last Activity     PC 2: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC3     PC 3: Status     PC 3: Date of First log     PC 3: Date of Last log     PC 3: Days since Last Activity     PC 3: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC4     PC 4: Status     PC 4: Date of First log     PC 4: Date of Last log     PC 4: Days since Last Activity     PC 4: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC5     PC 5: Status     PC 5: Date of First log     PC 5: Date of Last log     PC 5: Days since Last Activity     PC 5: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC6     PC 6: Status     PC 6: Date of First log     PC 6: Date of Last log     PC 6: Days since Last Activity     PC 6: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC7     PC 7: Status     PC 7: Date of First log     PC 7: Date of Last log     PC 7: Days since Last Activity     PC 7: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC8     PC 8: Status     PC 8: Date of First log     PC 8: Date of Last log     PC 8: Days since Last Activity     PC 8: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC9     PC 9: Status     PC 9: Date of First log     PC 9: Date of Last log     PC 9: Days since Last Activity     PC 9: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC10     PC 10: Status     PC 10: Date of First log     PC 10: Date of Last log     PC 10: Days since Last Activity     PC 10: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC11     PC 11: Status     PC 11: Date of First log     PC 11: Date of Last log     PC 11: Days since Last Activity     PC 11: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC12     PC 12: Status     PC 12: Date of First log     PC 12: Date of Last log     PC 12: Days since Last Activity     PC 12: Computer ID     Installation Date/Time MM/DD/YYYY HH:MI PC13     PC!
    *13: Sta                                                                                                                                                                                                                                                                                                                                                                                                                               *
    tus     PC 13: Date of First log     PC 13: Date of Last log     PC 13: Days since Last Activity     PC 13: Computer ID                                                                                                                                                                     
    The one highlighted in bold is the broken header.
    Please provide a solution to this.
    Thanks
    Monika
    Edited by: 953348 on Aug 18, 2012 8:00 AM

    I can't provide a solution but maybe a little help. With the header line you posted here it seems to me that a new line starts after 2048 = 2^11 characters. This might be a limitation or a length of any variable in the package.
    Maybe you can change this value or shorten the texts in the header to stay below this limit.
    A few more things:
    - the code, that you posted here became destroyed in lines like UTL_SMTP.mail (conn, '< ' .... because the forum software tried to find html-code between the "<" and ">" signs.
    - you can mark code in your postings with (see FAQ). It makes code better readable but it does not help with the "<" and ">" signs.
    - you left your e-mail-address in the header of the code. I guess this was not your intention. You can edit it and remove the address even after posting a message.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Sending Mails to Multiple Recipients using UTL_SMTP

    create or replace procedure send_mail(msg_text varchar2) is
    c utl_smtp.connection;
    PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS
    BEGIN
    utl_smtp.write_data(c, name || ': ' || header || utl_tcp.CRLF);
    END;
    BEGIN
    c := utl_smtp.open_connection('outlook.abc.com');
    utl_smtp.helo(c, 'abc.com');
    utl_smtp.mail(c, '[email protected]');
    utl_smtp.rcpt(c, '[email protected]');
    utl_smtp.open_data(c);
    send_header('From', '"root" <[email protected]>');
    send_header('To', '"abc" <[email protected]>');
    send_header('Subject', 'WARNING: Salary has been changed');
    utl_smtp.write_data(c, utl_tcp.CRLF || msg_text);
    utl_smtp.close_data(c);
    utl_smtp.quit(c);
    EXCEPTION
    WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
    BEGIN
    utl_smtp.quit(c);
    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 will raise an
    -- exception that we can ignore.
    END;
    raise_application_error(-20000,
    'Failed to send mail due to the following error: ' || sqlerrm);
    END;
    ==============
    when I execute the above using
    sql> exec send_mail('hihihih');
    I am getting the mails..no problems...
    So..I created the following trigger and used the above procedure to send the mail...
    CREATE OR REPLACE TRIGGER test_emp_table_trg
    AFTER UPDATE
    ON test_emp_table
    FOR EACH ROW
    WHEN (NEW.sal <> OLD.sal)
    DECLARE
    l_employee_name VARCHAR2 (240);
    l_old_sal VARCHAR2 (240);
    l_new_sal VARCHAR2 (240);
    l_message VARCHAR2 (240);
    BEGIN
    /* Gets the employee full name */
    BEGIN
    SELECT ename
    INTO l_employee_name
    FROM test_emp_table
    WHERE empno = :OLD.empno;
    EXCEPTION
    WHEN OTHERS
    THEN
    l_employee_name := NULL;
    END;
    /* Gets the old Salary */
    BEGIN
    SELECT sal
    INTO l_old_sal
    FROM test_emp_table
    WHERE empno = :OLD.empno;
    EXCEPTION
    WHEN OTHERS
    THEN
    l_old_sal := 0;
    END;
    /* Gets the new salary */
    BEGIN
    SELECT sal
    INTO l_new_sal
    FROM test_emp_table
    WHERE empno= :NEW.empno;
    EXCEPTION
    WHEN OTHERS
    THEN
    l_new_sal := 0;
    END;
    l_message:=
    'Employee Name= '
    || l_employee_name
    || 'Old Salary= '
    || l_old_sal
    || 'New Salary= '
    || l_new_sal;
    BEGIN
    send_mail (l_message);
    END;
    END;
    ===================
    I am not getting desired output..what might be problem friends...I am getting 0 values for old salary and new salary......
    One more thing..when i use 2 receipts in the send_mail procedure like this...I added the following lines in the procedure to send to multiple receipents..
    ======
    utl_smtp.rcpt(c, '[email protected]');
    utl_smtp.rcpt(c, '[email protected]');
    =============
    Pleas have a look and correct me, where i went wrong....
    Edited by: oraDBA2 on Sep 22, 2008 3:12 PM

    Hi, You can use the following routine to send mail to multiple recipients through utl_smtp.
    create or replace package mail_pkg
    as
    type array is table of varchar2(255);
    procedure send( p_sender_e_mail in varchar2,
    p_from in varchar2,
    p_to in array default array(),
    p_cc in array default array(),
    p_bcc in array default array(),
    p_subject in varchar2,
    p_body in long );
    end;
    create or replace package body mail_pkg
    begin
    g_crlf char(2) default chr(13)||chr(10);
    g_mail_conn utl_smtp.connection;
    g_mailhost varchar2(255) := 'ur mail server';
    function address_email( p_string in varchar2,
    p_recipients in array ) return varchar2
    is
    l_recipients long;
    begin
    for i in 1 .. p_recipients.count
    loop
    utl_smtp.rcpt(g_mail_conn, p_recipients(i));
    if ( l_recipients is null )
    then
    l_recipients := p_string || p_recipients(i) ;
    else
    l_recipients := l_recipients || ', ' || p_recipients(i);
    end if;
    end loop;
    return l_recipients;
    end;
    procedure send( p_sender_e_mail in varchar2,
    p_from in varchar2,
    p_to in array default array(),
    p_cc in array default array(),
    p_bcc in array default array(),
    p_subject in varchar2,
    p_body in long );
    end;
    is
    l_to_list long;
    l_cc_list long;
    l_bcc_list long;
    l_date varchar2(255) default
    to_char( SYSDATE, 'dd Mon yy hh24:mi:ss' );
    procedure writeData( p_text in varchar2 )
    as
    begin
    if ( p_text is not null )
    then
    utl_smtp.write_data( g_mail_conn, p_text || g_crlf );
    end if;
    end;
    begin
    g_mail_conn := utl_smtp.open_connection(g_mailhost, 25);
    utl_smtp.helo(g_mail_conn, g_mailhost);
    utl_smtp.mail(g_mail_conn, p_sender_e_mail);
    l_to_list := address_email( 'To: ', p_to );
    l_cc_list := address_email( 'Cc: ', p_cc );
    l_bcc_list := address_email( 'Bcc: ', p_bcc );
    utl_smtp.open_data(g_mail_conn );
    writeData( 'Date: ' || l_date );
    writeData( 'From: ' || nvl( p_from, p_sender_e_mail ) );
    writeData( 'Subject: ' || nvl( p_subject, '(no subject)' ) );
    writeData( l_to_list );
    writeData( l_cc_list );
    utl_smtp.write_data( g_mail_conn, '' || g_crlf );
    utl_smtp.write_data(g_mail_conn, p_body );
    utl_smtp.close_data(g_mail_conn );
    utl_smtp.quit(g_mail_conn);
    end;
    end;
    begin
    mail_pkg.send
    (p_sender_e_mail => 'urmail',
    p_from => 'urmail',
    p_to => mail_pkg.array( 'urmail','othersmail' ),
    p_cc => mail_pkg.array( ' othermail ' ),
    p_bcc => mail_pkg.array( '' ),
    p_subject => 'This is a subject',
    p_body => 'Hello Buddy, this is the mail you need' );
    end;
    /

  • Sending UTL_SMTP mail with Multiple attachment

    Hi,
    My Environment ----> Oracle Database 11g r1 on Windows 2003 Server (64Bit).
    The below script i used for sending mail with single attachment now i am trying to send mail with multiple attachment please tell me how to achieve this
    DECLARE
    /*LOB operation related varriables */
    v_src_loc BFILE := BFILENAME('DATA_PUMP_DIR', 'EXPORT.LOG');
    l_buffer RAW(54);
    l_amount BINARY_INTEGER := 54;
    l_pos INTEGER := 1;
    l_blob BLOB := EMPTY_BLOB;
    l_blob_len INTEGER;
    v_amount INTEGER;
    /*UTL_SMTP related varriavles. */
    v_connection_handle UTL_SMTP.CONNECTION;
    v_from_email_address VARCHAR2(30) := '[email protected]';
    v_to_email_address VARCHAR2(30) := '[email protected]';
    v_smtp_host VARCHAR2(30) := 'MAIL.EXPORT.COM'; --My mail server, replace it with yours.
    v_subject VARCHAR2(30) := 'MULTIPLE Attachment Test';
    l_message VARCHAR2(200) := 'TEST Mail for Multiple Attachment';
    /* This send_header procedure is written in the documentation */
    PROCEDURE send_header(pi_name IN VARCHAR2, pi_header IN VARCHAR2) AS
    BEGIN
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    pi_name || ': ' || pi_header || UTL_TCP.CRLF);
    END;
    BEGIN
    /*Preparing the LOB from file for attachment. */
    DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
    DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
    v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
    DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
    l_blob_len := DBMS_LOB.getlength(l_blob);
    /*UTL_SMTP related coding. */
    v_connection_handle := UTL_SMTP.OPEN_CONNECTION(host => v_smtp_host);
    UTL_SMTP.HELO(v_connection_handle, v_smtp_host);
    UTL_SMTP.MAIL(v_connection_handle, v_from_email_address);
    UTL_SMTP.RCPT(v_connection_handle, v_to_email_address);
    UTL_SMTP.OPEN_DATA(v_connection_handle);
    send_header('From', '"Sender"');
    send_header('To', '"Recipient"');
    send_header('Subject', v_subject);
    --MIME header.
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'MIME-Version: 1.0' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: multipart/mixed; ' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' boundary= "' || 'SAUBHIK.SECBOUND' || '"' ||
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Mail Body
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: text/plain;' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' charset=US-ASCII' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, l_message || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Mail Attachment
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: application/octet-stream' ||
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Disposition: attachment; ' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' filename="' || 'export.log' || '"' || --My filename
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    /* Writing the BLOL in chunks */
    WHILE l_pos < l_blob_len LOOP
    DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
    UTL_SMTP.write_raw_data(v_connection_handle,
    UTL_ENCODE.BASE64_ENCODE(l_buffer));
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    l_buffer := NULL;
    l_pos := l_pos + l_amount;
    END LOOP;
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Close Email
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || '--' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    UTL_TCP.CRLF || '.' || UTL_TCP.CRLF);
    UTL_SMTP.CLOSE_DATA(v_connection_handle);
    UTL_SMTP.QUIT(v_connection_handle);
    DBMS_LOB.FREETEMPORARY(l_blob);
    DBMS_LOB.FILECLOSE(v_src_loc);
    EXCEPTION
    WHEN OTHERS THEN
    UTL_SMTP.QUIT(v_connection_handle);
    DBMS_LOB.FREETEMPORARY(l_blob);
    DBMS_LOB.FILECLOSE(v_src_loc);
    RAISE;
    END;
    Thank you
    Shan

    Hi Saubhik
    Thanks for your reply, below script i used to send mail with multiple attachments, plsql code is executing without any error messages and i am also able to receive mail with the multiple attachment.
    i used your code which u posted in OTN then i changed little bit as per my need, output is ok.but the problem is if i want to add one more file then i have to add more varaiables in the code. i want to make the code which i can add more attachments without adding more varaiables i don't know the way to do this. can u please give me some hints.
    Thanks for your help
    Shan
    Script Used:
    DECLARE
    /*LOB operation related varriables01 */
    v_src_loc BFILE := BFILENAME('DATA_PUMP_DIR', 'EXPORT.LOG');
    l_buffer RAW(54);
    l_amount BINARY_INTEGER := 54;
    l_pos INTEGER := 1;
    l_blob BLOB := EMPTY_BLOB;
    l_blob_len INTEGER;
    v_amount INTEGER;
    /*LOB operation related varriables02 */
    v_src_loc2 BFILE := BFILENAME('DATA_PUMP_DIR', 'EXPORT1.LOG');
    l_buffer2 RAW(54);
    l_amount2 BINARY_INTEGER := 54;
    l_pos2 INTEGER := 1;
    l_blob2 BLOB := EMPTY_BLOB;
    l_blob_len2 INTEGER;
    v_amount2 INTEGER;
    /*UTL_SMTP related varriavles. */
    v_connection_handle UTL_SMTP.CONNECTION;
    v_from_email_address VARCHAR2(30) := '[email protected]';
    v_to_email_address VARCHAR2(30) := '[email protected]';
    v_smtp_host VARCHAR2(30) := 'MAIL.EXPORT.COM'; --My mail server, replace it with yours.
    v_subject VARCHAR2(30) := 'MULTIPLE Attachment Test';
    l_message VARCHAR2(200) := 'TEST Mail for Multiple Attachment';
    /* This send_header procedure is written in the documentation */
    PROCEDURE send_header(pi_name IN VARCHAR2, pi_header IN VARCHAR2) AS
    BEGIN
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    pi_name || ': ' || pi_header || UTL_TCP.CRLF);
    END;
    BEGIN
    /*Preparing the LOB from file for attachment01. */
    DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
    DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
    v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
    DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
    l_blob_len := DBMS_LOB.getlength(l_blob);
    /*Preparing the LOB from file for attachment02. */
    DBMS_LOB.OPEN(v_src_loc2, DBMS_LOB.LOB_READONLY); --Read the file
    DBMS_LOB.CREATETEMPORARY(l_blob2, TRUE); --Create temporary LOB to store the file.
    v_amount2 := DBMS_LOB.GETLENGTH(v_src_loc2); --Amount to store.
    DBMS_LOB.LOADFROMFILE(l_blob2, v_src_loc2, v_amount2); -- Loading from file into temporary LOB
    l_blob_len2 := DBMS_LOB.getlength(l_blob2);
    /*UTL_SMTP related coding. */
    v_connection_handle := UTL_SMTP.OPEN_CONNECTION(host => v_smtp_host);
    UTL_SMTP.HELO(v_connection_handle, v_smtp_host);
    UTL_SMTP.MAIL(v_connection_handle, v_from_email_address);
    UTL_SMTP.RCPT(v_connection_handle, v_to_email_address);
    UTL_SMTP.OPEN_DATA(v_connection_handle);
    send_header('From', '"Sender"');
    send_header('To', '"Recipient"');
    send_header('Subject', v_subject);
    --MIME header.
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'MIME-Version: 1.0' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: multipart/mixed; ' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' boundary= "' || 'SAUBHIK.SECBOUND' || '"' ||
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Mail Body
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: text/plain;' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' charset=US-ASCII' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, l_message || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Mail Attachment01
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: application/octet-stream' ||
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Disposition: attachment; ' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' filename="' || 'export.log' || '"' || --My filename
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    /* Writing the BLOL in chunks */
    WHILE l_pos < l_blob_len LOOP
    DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
    UTL_SMTP.write_raw_data(v_connection_handle,
    UTL_ENCODE.BASE64_ENCODE(l_buffer));
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    l_buffer := NULL;
    l_pos := l_pos + l_amount;
    END LOOP;
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Mail Attachment02
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Type: application/octet-stream' ||
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Disposition: attachment; ' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    ' filename="' || 'export1.log' || '"' || --My filename
    UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    /* Writing the BLOL in chunks */
    WHILE l_pos2 < l_blob_len2 LOOP
    DBMS_LOB.READ(l_blob2, l_amount2, l_pos2, l_buffer2);
    UTL_SMTP.write_raw_data(v_connection_handle,
    UTL_ENCODE.BASE64_ENCODE(l_buffer2));
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    l_buffer2 := NULL;
    l_pos2 := l_pos2 + l_amount2;
    END LOOP;
    UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
    -- Close Email
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    '--' || 'SAUBHIK.SECBOUND' || '--' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
    UTL_TCP.CRLF || '.' || UTL_TCP.CRLF);
    UTL_SMTP.CLOSE_DATA(v_connection_handle);
    UTL_SMTP.QUIT(v_connection_handle);
    DBMS_LOB.FREETEMPORARY(l_blob);
    DBMS_LOB.FILECLOSE(v_src_loc);
    DBMS_LOB.FREETEMPORARY(l_blob2);
    DBMS_LOB.FILECLOSE(v_src_loc2);
    EXCEPTION
    WHEN OTHERS THEN
    UTL_SMTP.QUIT(v_connection_handle);
    DBMS_LOB.FREETEMPORARY(l_blob);
    DBMS_LOB.FILECLOSE(v_src_loc);
    DBMS_LOB.FREETEMPORARY(l_blob2);
    DBMS_LOB.FILECLOSE(v_src_loc2);
    RAISE;
    END;
    PL/SQL procedure successfully completed.
    Edited by: SHAN2009 on May 11, 2011 1:05 PM

  • Sending PDF as attachment using utl_smtp

    Hi all,
    I am encountering the following problem when i try to send the email using utl_smtp builtin,i receive the mail in my outlook,but not able to read the contents of the pdf file.
    Oracle Version :- Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    source code:-
    CREATE OR REPLACE PROCEDURE "PROC_MAIL_ATTACH_PDFS" (
    p_cust_nb IN cstm.cstm_cust_nb%TYPE,
    from_name IN VARCHAR2,
    to_name IN VARCHAR2,
    subject IN VARCHAR2,
    MESSAGE IN VARCHAR2,
    p_binary_file IN VARCHAR2,
    p_seq_id IN NUMBER,
    p_ret_cd OUT NUMBER,
    p_ret_desc OUT VARCHAR2
    --ISO-8859-6
    IS
    v_smtp_server VARCHAR2 (100) := '172.20.204.17';
    --change this to your mail server
    v_smtp_server_port NUMBER := 25;
    v_directory_name VARCHAR2 (100);
    v_file_name VARCHAR2 (100);
    v_line VARCHAR2 (1000);
    crlf VARCHAR2 (2) := CHR (13)
    || CHR (10);
    mesg VARCHAR2 (32767);
    conn UTL_SMTP.connection;
    v_slash_pos NUMBER;
    v_file_handle UTL_FILE.file_type;
    invalid_path EXCEPTION;
    mesg_length_exceeded BOOLEAN := FALSE;
    l_msg VARCHAR2 (32000);
    l_tag_sep VARCHAR2 (1)
    := func_get_config_value ('TAGSEPRTR');
    l_ind VARCHAR2 (10);
    l_rec VARCHAR2 (4000);
    l_sep_rep VARCHAR2 (4000) := '17_ACCSTMT_NOV_2010';
    l_rem_rep VARCHAR2 (4000);
    l_rep VARCHAR2 (4000);
    l_mim_type VARCHAR2 (2000);
    boundary CONSTANT VARCHAR2 (256)
    := '-----DMW.Boundary.605592468';
    first_boundary CONSTANT VARCHAR2 (256) := '--' || boundary || crlf;
    last_boundary CONSTANT VARCHAR2 (256)
    := '--' || boundary || '--' || crlf;
    multipart_mime_type CONSTANT VARCHAR2 (256)
    := 'multipart/mixed; boundary="' || boundary || '"';
    mime_type VARCHAR2 (255) := 'text/html';
    l_offset NUMBER;
    l_ammount NUMBER;
    CURSOR cur_trnm
    IS
    SELECT trnm_email_enarr, trnm_email_anarr
    FROM trnm
    WHERE trnm_id = 16;
    l_enarr trnm.trnm_email_enarr%TYPE;
    l_anarr trnm.trnm_email_enarr%TYPE;
    l_message_body VARCHAR2 (32000);
    --// To check if all mails belongs to the customer already sent...
    CURSOR cur_pdfd
    IS
    SELECT COUNT (*)
    FROM pdfd
    WHERE pdfd_cust_nb = p_cust_nb
    AND pdfd_email_sts IS NULL
    --NEWLY ADDED ...
    AND NVL (pdfd_stmt_mode, '.') = 'E'
    AND TRUNC (pdfd_to_dt) = (SELECT MAX (TRUNC (pdfd_to_dt))
    FROM pdfd
    WHERE pdfd_cust_nb = p_cust_nb);
    l_cnt NUMBER := 0;
    PROCEDURE send_header (NAME IN VARCHAR2, header IN VARCHAR2)
    IS
    BEGIN
    UTL_SMTP.write_data (conn, NAME || ': ' || header || crlf);
    END;
    PROCEDURE write_raw (
    p_conn IN OUT NOCOPY UTL_SMTP.connection,
    p_message IN RAW
    IS
    BEGIN
    UTL_SMTP.write_raw_data (p_conn, p_message);
    END write_raw;
    PROCEDURE binary_attachment (
    p_conn IN OUT UTL_SMTP.connection,
    p_file_name IN VARCHAR2,
    p_mime_type IN VARCHAR2
    IS
    k_max_line_width CONSTANT PLS_INTEGER DEFAULT 54;
    v_amt BINARY_INTEGER := 672 * 3;
    /* ensures proper format; 2016 */
    v_bfile BFILE;
    v_file_len PLS_INTEGER;
    v_buf RAW (2100);
    v_buf1 RAW (2100);
    v_modulo PLS_INTEGER;
    v_pieces PLS_INTEGER;
    v_file_pos PLS_INTEGER := 1;
    v_data RAW (32767);
    v_data1 RAW (32767);
    v_chunks PLS_INTEGER;
    l_amt NUMBER := 32767;
    l_off NUMBER := 1;
    l_raw RAW (32767);
    l_raw1 RAW (32767);
    l_lob BLOB;
    l_lob_empt BLOB;
    req UTL_HTTP.req;
    resp UTL_HTTP.resp;
    resp_empt UTL_HTTP.resp;
    l_url VARCHAR2 (4000);
    l_rep_path VARCHAR2 (2000);
    l_report VARCHAR2 (100);
    l_seq_nb repq.repq_seq_nb%TYPE;
    l_parm repq.repq_parm_val%TYPE;
    l_repq repq%ROWTYPE;
    l_sts NUMBER;
    l_user VARCHAR2 (10);
    --L_MSG VARCHAR2(32000);
    l_seq_id NUMBER;
    usr_err EXCEPTION;
    --// 07-Jun-2009 - Basheer A.S. : Code added for sending A/c Statement and PFL statements in single e-mail ...
    CURSOR cur_pdfd
    IS
    SELECT
    --UTL_COMPRESS.LZ_UNCOMPRESS(PDFD_DB_FILE) PDFD_DB_FILE,
    pdfd_file_name
    FROM pdfd
    WHERE pdfd_cust_nb = p_cust_nb
    --AND PDFD_EMAIL_STS IS NULL
    --NEWLY ADDED ...
    --AND NVL(PDFD_STMT_MODE,'.') = 'E'
    AND pdfd_seq_nb = p_seq_id
    AND TRUNC (pdfd_to_dt) = (SELECT MAX (TRUNC (pdfd_to_dt))
    FROM pdfd
    WHERE pdfd_cust_nb = p_cust_nb);
    l_buffer_size INTEGER := 57;
    l_offset INTEGER := 1;
    l_raw RAW (57);
    l_file_nm pdfd.pdfd_file_name%TYPE;
    BEGIN
    --// 06-Jun-2009 - Basheer A.S. : Code added for sending A/c Statement and PFL statements in a single e-mail ...
    --// Initializing temporary CLOB data ...
    DBMS_LOB.createtemporary (l_lob, FALSE);
    --DBMS_LOB.createtemporary(L_LOB_EMPT, FALSE);
    --// Loop thro all the records for the given Customer Number...
    OPEN cur_pdfd;
    LOOP
    --FETCH CUR_PDFD INTO L_LOB, L_FILE_NM;
    FETCH cur_pdfd
    INTO --L_LOB_EMPT,
    l_file_nm;
    EXIT WHEN cur_pdfd%NOTFOUND;
    proc_audit_log ('T',
    'PROC_MAIL_ATTACH_PDFS, Customer No. '
    || p_cust_nb
    || ', File attachment: '
    || l_file_nm
    UTL_SMTP.write_data (conn, first_boundary);
    UTL_SMTP.write_data (conn,
    'Content-Transfer-Encoding: base64 '
    || UTL_TCP.crlf
    UTL_SMTP.write_data (conn,
    'Content-Type: ' || mime_type || UTL_TCP.crlf
    UTL_SMTP.write_data (conn,
    'Content-Disposition: ATTACHMENT; filename="'
    || p_file_name
    || '"'
    || UTL_TCP.crlf
    UTL_SMTP.write_data (conn, crlf);
    l_ind := '1.1';
    v_file_pos := 1;
    --// Attaching individual PDF files ...
    BEGIN
    v_modulo := 0;
    v_pieces := 0;
    v_amt := 2016;
    l_ind := '2.1';
    v_file_len := DBMS_LOB.getlength (l_lob);
    --v_file_len := dbms_lob.getlength(L_LOB_EMPT);
    v_modulo := MOD (v_file_len, v_amt);
    v_pieces := TRUNC (v_file_len / v_amt);
    IF (v_modulo <> 0)
    THEN
    v_pieces := v_pieces + 1;
    END IF;
    l_ind := '2.2';
    DBMS_LOB.READ (l_lob, v_amt, v_file_pos, v_buf);
    --dbms_lob.read(L_LOB_EMPT, v_amt, v_file_pos, v_buf);
    v_data := v_data1;
    v_chunks := 0;
    v_data := v_data1;
    FOR i IN 1 .. v_pieces
    LOOP
    v_file_pos := i * v_amt + 1;
    v_file_len := v_file_len - v_amt;
    v_data := UTL_RAW.CONCAT (v_data, v_buf);
    l_ind := '2.3';
    v_chunks := TRUNC (UTL_RAW.LENGTH (v_data) / k_max_line_width);
    IF (i <> v_pieces)
    THEN
    v_chunks := v_chunks - 1;
    END IF;
    l_ind := '2.4';
    write_raw (p_conn => p_conn,
    p_message => UTL_ENCODE.base64_encode (v_data)
    v_data := v_data1;
    IF (v_file_len < v_amt AND v_file_len > 0)
    THEN
    v_amt := v_file_len;
    END IF;
    l_ind := '2.5';
    DBMS_LOB.READ (l_lob, v_amt, v_file_pos, v_buf);
    --DBMS_LOB.READ(L_LOB_EMPT, v_amt, v_file_pos, v_buf);
    END LOOP;
    EXCEPTION
    WHEN OTHERS
    THEN
    proc_audit_log
    ('E',
    'PROC_MAIL_ATTACH_PDFS.binary_attachment.inside BLOB loop, :'
    || SQLERRM
    || ',ind:'
    || l_ind
    END;
    v_file_pos := 1;
    UTL_SMTP.write_data (conn, UTL_TCP.crlf);
    UTL_SMTP.write_data (conn, UTL_TCP.crlf);
    END LOOP;
    --// END multiple file attachments
    l_ind := '2.6';
    UTL_SMTP.write_data (p_conn, last_boundary || UTL_TCP.crlf);
    EXCEPTION
    WHEN OTHERS
    THEN
    proc_audit_log ('E',
    'PROC_MAIL_ATTACH_PDFS, when others:'
    || l_ind
    || ', '
    || SQLERRM
    END binary_attachment;
    BEGIN
    --// If no pending emails for the given Customer, then exit the process...
    OPEN cur_pdfd;
    FETCH cur_pdfd
    INTO l_cnt;
    CLOSE cur_pdfd;
    --// If still pending statements needs to be send...
    --IF L_CNT > 0 THEN
    IF l_cnt = 0
    THEN
    OPEN cur_trnm;
    FETCH cur_trnm
    INTO l_enarr, l_anarr;
    CLOSE cur_trnm;
    l_message_body :=
    '<html>'
    ||
    --'<meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-1256(Arabic)">'||
    --'<meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-1256">'||
    '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-6">'
    || '<body>
    <TABLE BORDER="0" WIDTH=100% style="table-layout:fixed;">
    <TR>
    <TD WIDTH="50%" ALIGN="LEFT" style="word-wrap: break-word"><B>'
    || l_enarr
    || '</B></TD>'
    || '<TD WIDTH="50%" ALIGN="RIGHT" style="word-wrap: break-word"><B>'
    || l_anarr
    || '</B></TD></TR></TABLE>
    </body>
    </html>';
    proc_audit_log ('T', 'PROC_MAIL_ATTACH_PDFS, msg:' || l_msg);
    v_smtp_server := func_get_config_value ('MAILHOST');
    v_smtp_server_port := func_get_config_value ('MAILPORT');
    l_ind := '3.1';
    conn := UTL_SMTP.open_connection (v_smtp_server, v_smtp_server_port);
    --utl_smtp.helo( conn, v_smtp_server );
    --utl_smtp.mail( conn, '[email protected]' );
    UTL_SMTP.helo (conn, v_smtp_server);
    UTL_SMTP.mail (conn, from_name);
    UTL_SMTP.rcpt (conn, to_name);
    l_ind := '3.2';
    l_rec := func_eti_tagval (l_msg, 'EMAIL');
    proc_audit_log ('T', 'PROC_MAIL_ATTACH_PDFS, l_rec:' || l_rec);
    proc_audit_log ('T', 'l_sep_rep1' || l_sep_rep);
    UTL_SMTP.open_data (conn);
    send_header ('From', '<' || from_name || '>');
    send_header ('To', '<' || to_name || '>');
    --send_header('To',''||Func_Eti_Tagval(L_MSG, 'EMAIL')||'');
    send_header ('Date', TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss'));
    send_header ('Subject', subject);
    send_header ('Content-Type', multipart_mime_type);
    UTL_SMTP.write_data (conn, first_boundary);
    --utl_smtp.write_data(conn, 'Content-Type: '||mime_type||utl_tcp.crlf);
    --utl_smtp.write_data(conn, 'Content-Type: '||mime_type||'; charset=Windows-1256'||utl_tcp.crlf);
    UTL_SMTP.write_data (conn,
    'Content-Type: '
    || mime_type
    || '; charset=ISO-8859-6'
    || UTL_TCP.crlf
    --new...
    UTL_SMTP.write_data (conn, crlf);
    UTL_SMTP.write_raw_data (conn, UTL_RAW.cast_to_raw (l_message_body));
    UTL_SMTP.write_data (conn, crlf);
    UTL_SMTP.write_data (conn, crlf);
    proc_audit_log ('T', 'l_sep_rep2' || l_sep_rep);
    binary_attachment (p_conn => conn,
    p_file_name => l_sep_rep || '.pdf',
    p_mime_type => 'multipart/mixed'
    ); --||l_sep_rep||'.pdf');
    UTL_SMTP.write_data (conn, last_boundary || UTL_TCP.crlf);
    UTL_SMTP.close_data (conn);
    UTL_SMTP.quit (conn);
    END IF;
    p_ret_cd := 0;
    p_ret_desc := 'Mail sent successfully';
    EXCEPTION
    WHEN OTHERS
    THEN
    proc_audit_log ('E',
    'PROC_MAIL_ATTACH_PDFS, error:'
    || SQLERRM
    || 'ind:'
    || l_ind
    p_ret_cd := 1;
    END;
    Kindly help me to resolve this issue.
    Thanks & Regards
    Ariff

    pl ease check below link
    /people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface

  • Sending text Attachment using UTL_SMTP

    Hi,
    I am using UTL_SMTP package for sending a text file thru' a mail.
    Our database is in a unix machine. My file is created in a folder oracle/DEV/CCare/Data which is in the root directory.
    In the URL argument of the procedure,which is a varchar2 type, i am passing the path as 'oracle/DEV/CCare/Data'. Also i have tried passing '/oracle/DEV/CCare/Data' like this. But none of them is working could anybody suggest way of passing the URL.
    The package arguments look like below.
    oks_mail.send_attachment(sender => l_fromemailid,
    recipient_tbl => to_mail_tbl,
    subject => 'Progress report for CCC Feed',
    mail_text => l_mailtext1,
    url => '//10.10.81.3/oracle/DEV/CCare/Data',
    file_name => filename);

    Use
    FILE_ARRAY(1) := file_name;
    RETURN_DESC1 := '10 - E: THERE WAS AN ERROR IN OPENING CONNECTION. ';
    CONN:= UTL_SMTP.OPEN_CONNECTION( L_SMTP_SERVER, L_SMTP_SERVER_PORT ); /** OPEN CONNECTION ON THE SERVER **/
    UTL_SMTP.HELO( CONN, L_SMTP_SERVER ); /** DO THE INITIAL HAND SHAKE **/
    UTL_SMTP.MAIL( CONN, L_SENDER_NAME );
    RETURN_DESC1 := '20 - E: THERE WAS AN ERROR IN CREATING RECEIPIENTS. ';
    UTL_SMTP.RCPT( CONN, '[email protected]');
    UTL_SMTP.OPEN_DATA ( CONN );
    --Send_header('Subject', 'File - ');
    /*** GENERATE THE MIME HEADER ***/
    RETURN_DESC1 := '30 - E: THERE WAS AN ERROR IN GENERATING MIME HEADER. ';
    L_MESG:= 'Date: ' || TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' )|| CRLF ||
    'From: ' || L_SENDER_NAME || CRLF ||
    'Subject: ' || file_name || CRLF ||
    'To: ' || L_MSG_TO || CRLF ||
    'Mime-Version: 1.0' || CRLF ||
    'Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"'|| CRLF ||
    '' || CRLF ||
    'This is a Mime message, which your current mail reader may not' || CRLF ||
    'understand. Parts of the message will appear as text. If the remainder' || CRLF ||
    'appears as random characters in the message body, instead of as' || CRLF ||
    'attachments, then you''ll have to extract these parts and decode them' || CRLF ||
    'manually.' || CRLF ||'' ;/*|| CRLF ||
    '--DMW.Boundary.605592468' || CRLF ||
    'Content-Type: text/plain; name="message.txt"; charset=US-ASCII' || CRLF ||
    'Content-Disposition: inline; filename="message.txt"' || CRLF ||
    'Content-Transfer-Encoding: 7bit' || CRLF ||
    '' || CRLF || CRLF || CRLF || CRLF ;*/
    L_MESG_LEN := LENGTH(L_MESG);
    IF L_MESG_LEN > MAX_SIZE THEN
    MESG_LENGTH_EXCEEDED := TRUE;
    END IF;
    RETURN_DESC1 := '40 - E: THERE WAS AN ERROR IN WRITING MESSAGE TO CONNECTION. ';
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    /*** START ATTACHING THE FILES ***/
    FOR I IN 1..1 LOOP
    EXIT WHEN MESG_LENGTH_EXCEEDED;
    IF FILE_ARRAY(I) IS NOT NULL THEN
    BEGIN
    L_SLASH_POS := INSTR(FILE_ARRAY(I), '/', -1 );
    IF L_SLASH_POS = 0 THEN
    L_SLASH_POS := INSTR(FILE_ARRAY(I), '\', -1 );
    END IF;
    L_DIRECTORY_NAME := SUBSTR(FILE_ARRAY(I), 1, L_SLASH_POS - 1 );
    L_FILE_NAME := SUBSTR(FILE_ARRAY(I), L_SLASH_POS + 1 );
    RETURN_DESC1 := '50 - E: THERE WAS AN ERROR IN OPENING FILE. ';
    L_FILE_HANDLE := UTL_FILE.FOPEN(L_DIRECTORY_NAME, L_FILE_NAME, 'R' );
    L_MESG := CRLF || '--DMW.Boundary.605592468' || CRLF ||
    'Content-Type: application/octet-stream; name="' || L_FILE_NAME || '"' || CRLF ||
    'Content-Disposition: attachment; filename="' || L_FILE_NAME || '"' || CRLF ||
    'Content-Transfer-Encoding: 7bit' || CRLF || CRLF ;
    L_MESG_LEN := L_MESG_LEN + LENGTH(L_MESG);
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    LOOP
    RETURN_DESC1 := '60 - E: THERE WAS AN ERROR IN READING FILE. ';
    UTL_FILE.GET_LINE(L_FILE_HANDLE, L_LINE);
    IF L_MESG_LEN + LENGTH(L_LINE) > MAX_SIZE THEN
    L_MESG := '*** truncated ***' || CRLF;
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    MESG_LENGTH_EXCEEDED := TRUE;
    EXIT;
    END IF;
    L_MESG := L_LINE || CRLF;
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    L_MESG_LEN := L_MESG_LEN + LENGTH(L_MESG);
    END LOOP;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    out_errcode:=-500;
    WHEN UTL_FILE.INVALID_PATH THEN
                        RAISE ABORT_PROGRAM;
    WHEN OTHERS THEN
    RAISE ABORT_PROGRAM;
    END;
    L_MESG := CRLF;
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    UTL_FILE.FCLOSE(L_FILE_HANDLE);
    END IF;
    END LOOP;
    RETURN_DESC1 := '70 - E: THERE WAS AN ERROR IN CLOSING MIME BOUNDARY. ';
    L_MESG := CRLF || '--DMW.Boundary.605592468--' || CRLF;
    UTL_SMTP.WRITE_DATA ( CONN, L_MESG );
    UTL_SMTP.CLOSE_DATA( CONN );
    UTL_SMTP.QUIT( CONN );

  • How to put an image to any part of an e-mail using UTL_SMTP

    We need to send an e-mail with the following format.
    |COMPANY LOGO (JPEC IMAGE)          |
    |                                    |
    |                                    |
    |              HTML table            |
    |                                    |
    |                                    |
    ------------------------------------The exact format is shown here: http://postimage.org/image/76v4e5tmd/
    Above the Automatic Payment Advice is the JPEG image.
    How do we CONSTRUCT THIS e-mail? Our DB is a 10g R2. We use UTL_SMTP. Problem is how to insert an image to any part of the e-mail (not as a separate attachment)?
    Edited by: Channa on May 24, 2012 5:51 AM

    Yes. It is possible. Read this posts of Billy Verreynne to uderstand the MIME format.
    Re: Sending HTML mail with inline/embeded images (My code is constructed on this input)
    embeded image in email body in pl/sql
    DECLARE
      /*LOB operation related varriables */
      v_src_loc  BFILE := BFILENAME('TEMP', 'otn.jpg');
      l_buffer   RAW(54);
      l_amount   BINARY_INTEGER := 54;
      l_pos      INTEGER := 1;
      l_blob     BLOB := EMPTY_BLOB;
      l_blob_len INTEGER;
      v_amount   INTEGER;
      /*UTL_SMTP related varriavles. */
      v_connection_handle  UTL_SMTP.CONNECTION;
      v_from_email_address VARCHAR2(30) := '[email protected]';
      v_to_email_address   VARCHAR2(30) := '[email protected]';
      v_smtp_host          VARCHAR2(30) := 'x.xxx.xxx.xxx'; --My mail server, replace it with yours.
      v_subject            VARCHAR2(30) := 'Your Test Mail';
      l_message            VARCHAR2(32767) := '<html>
    <meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dus-ascii">
    <body background=3D"cid:[email protected]">
    ..rest of mail
    </body>
    </html>
      /* This send_header procedure is written in the documentation */
      PROCEDURE send_header(pi_name IN VARCHAR2, pi_header IN VARCHAR2) AS
      BEGIN
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            pi_name || ': ' || pi_header || UTL_TCP.CRLF);
      END;
    BEGIN
      /*Preparing the LOB from file for attachment. */
      DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
      DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
      v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
      DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
      l_blob_len := DBMS_LOB.getlength(l_blob);
      /*UTL_SMTP related coding. */
      v_connection_handle := UTL_SMTP.OPEN_CONNECTION(host => v_smtp_host);
      UTL_SMTP.HELO(v_connection_handle, v_smtp_host);
      UTL_SMTP.MAIL(v_connection_handle, v_from_email_address);
      UTL_SMTP.RCPT(v_connection_handle, v_to_email_address);
      UTL_SMTP.OPEN_DATA(v_connection_handle);
      send_header('From', '"Sender" <' || v_from_email_address || '>');
      send_header('To', '"Recipient" <' || v_to_email_address || '>');
      send_header('Subject', v_subject);
      --MIME header.
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'MIME-Version: 1.0' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Type: multipart/related; ' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          ' boundary= "' || 'SAUBHIK.SECBOUND' || '"' ||
                          UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      -- Mail Body
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Type: text/html;' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          ' charset=US-ASCII' || UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Transfer-Encoding: quoted-printable' || UTL_TCP.CRLF);                     
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle, l_message || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      -- Mail Attachment
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Disposition: inline; filename="otn.jpg"' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Type: image/jpg; name="otn.jpg"' ||
                          UTL_TCP.CRLF);
    UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-ID: <[email protected]>; ' ||
                          UTL_TCP.CRLF);                     
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      /* Writing the BLOL in chunks */
      WHILE l_pos < l_blob_len LOOP
        DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
        UTL_SMTP.write_raw_data(v_connection_handle,
                                UTL_ENCODE.BASE64_ENCODE(l_buffer));
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        l_buffer := NULL;
        l_pos    := l_pos + l_amount;
      END LOOP;
      UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      -- Close Email
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          '--' || 'SAUBHIK.SECBOUND' || '--' || UTL_TCP.CRLF);
      UTL_SMTP.WRITE_DATA(v_connection_handle,
                          UTL_TCP.CRLF || '.' || UTL_TCP.CRLF);
      UTL_SMTP.CLOSE_DATA(v_connection_handle);
      UTL_SMTP.QUIT(v_connection_handle);
      DBMS_LOB.FREETEMPORARY(l_blob);
      DBMS_LOB.FILECLOSE(v_src_loc);
    EXCEPTION
      WHEN OTHERS THEN
        UTL_SMTP.QUIT(v_connection_handle);
        DBMS_LOB.FREETEMPORARY(l_blob);
        DBMS_LOB.FILECLOSE(v_src_loc);
        RAISE;
    END;Otn logo is in my database server and It will embade otn logo all over the mail body!.
    Edited by: Saubhik on May 24, 2012 9:06 PM
    Changed the original IP and email address. I should have done this earlier!: Saubhik on May 25, 2012 11:20 AM

  • UTL_SMTP mail with attachment( Problem in adding multiple recipient)

    Hi All,
    I am using the below code for sending the attachment.
    When i try to add the mail group name in my recipient details i am getting the following error
    ORA-29279: SMTP permanent error: 501 Syntax error, parameters in command "RCPT TO:Application Support - IT" unrecognized or missingHow to add the group in the recipient or cc ????
    My mail code is:
    create or replace PROCEDURE "SSSL_SEND_MAIL" ( p_sender varchar2,
          p_recipient varchar2,
          p_cc varchar2,
          p_subject varchar2,
          p_filename varchar2,
          text varchar2) is 
        /*LOB operation related varriables */
       v_src_loc  BFILE;
       l_buffer   RAW(54);
       l_amount   BINARY_INTEGER := 54;
       l_pos      INTEGER := 1;
       l_blob     BLOB := EMPTY_BLOB;
       l_blob_len INTEGER;
       v_amount   INTEGER;
        /*UTL_SMTP related varriavles. */
        v_connection_handle  UTL_SMTP.CONNECTION;
        v_from_email_address VARCHAR2(200);
        v_to_email_address   VARCHAR2(200) ;
        v_cc                 VARCHAR2(200);
        v_smtp_host          VARCHAR2(10) ;
        v_subject            VARCHAR2(500) ;
        l_message            VARCHAR2(3000);
        l_filename           VARCHAR2(4000);
        /* This send_header procedure is written in the documentation */
        PROCEDURE send_header(pi_name IN VARCHAR2, pi_header IN VARCHAR2) AS
        BEGIN
        --dbms_output.put_line('entering into procedure');
        --dbms_output.put_line(pi_name || ': ' || pi_header);
          UTL_SMTP.WRITE_DATA(v_connection_handle,
                              pi_name || ': ' || pi_header || UTL_TCP.CRLF);
        END;
      BEGIN
       v_src_loc             := BFILENAME('BROKERREPORTS',p_filename);
       v_from_email_address  := p_sender;
       v_to_email_address    := p_recipient;
       v_cc                  := p_cc;
       v_smtp_host           := 'xxxxxx'; --My mail server, replace it with yours.
       v_subject             := p_subject;
       l_message      := 'tets';
        /*Preparing the LOB from file for attachment. */
        DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
        DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
        v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
        DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
        l_blob_len := DBMS_LOB.getlength(l_blob);
        l_filename:=p_filename;
        IF l_blob_len > 7000000 THEN
        BEGIN
        SSSL_FILE_ZIP.COMPRESSFILE(
        P_IN_FILE => '/mfundb/prdapex/BROKER-REPORTS/'||p_filename,
        P_OUT_FILE => '/mfundb/prdapex/BROKER-REPORTS/'||REPLACE(UPPER(p_filename),'XLS','ZIP')
      l_filename:=NULL;
      l_filename:= REPLACE(UPPER(p_filename),'XLS','ZIP');
      v_src_loc := BFILENAME('BROKERREPORTS',l_filename);
      DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
      DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
      v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
      DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
      l_blob_len := DBMS_LOB.getlength(l_blob);
    EXCEPTION WHEN OTHERS THEN
    sssl_internal_error_track(sqlcode,sqlerrm,'SSSL_FILE_ZIP',l_filename||'-'||p_recipient);
    END;
    END IF;
    /*UTL_SMTP related coding. */
        v_connection_handle := UTL_SMTP.OPEN_CONNECTION(host => v_smtp_host);
        UTL_SMTP.HELO(v_connection_handle, v_smtp_host);
        UTL_SMTP.MAIL(v_connection_handle, v_from_email_address);
        UTL_SMTP.RCPT(v_connection_handle, v_to_email_address);
        UTL_SMTP.RCPT(v_connection_handle, v_cc);
        UTL_SMTP.OPEN_DATA(v_connection_handle);
        send_header('From', v_from_email_address);-- || '<'||'>');
        send_header('TO', v_to_email_address);--|| '<'||'>');
        send_header('CC', v_cc );   
        send_header('Subject', v_subject);
        --MIME header.
       UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'MIME-Version: 1.0' || UTL_TCP.CRLF);
       UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Type: multipart/mixed; ' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' boundary= "' || 'SAUBHIK.SECBOUND' || '"' ||
                            UTL_TCP.CRLF);
       UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Mail Body
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Type: text/plain;' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' charset=US-ASCII' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, l_message || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Mail Attachment
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                           'Content-Type: application/octet-stream' ||
                            UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Disposition: attachment; ' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' filename="' || l_filename || '"' || --My filename
                            UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      /* Writing the BLOL in chunks */
        WHILE l_pos < l_blob_len LOOP
          DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
          UTL_SMTP.write_raw_data(v_connection_handle,
                                  UTL_ENCODE.BASE64_ENCODE(l_buffer));
          UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
          l_buffer := NULL;
          l_pos    := l_pos + l_amount;
        END LOOP;
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Close Email
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || '--' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            UTL_TCP.CRLF || '.' || UTL_TCP.CRLF);
        UTL_SMTP.CLOSE_DATA(v_connection_handle);
        UTL_SMTP.QUIT(v_connection_handle);
        DBMS_LOB.FREETEMPORARY(l_blob);
        DBMS_LOB.FILECLOSE(v_src_loc);
      EXCEPTION
        WHEN OTHERS THEN
          UTL_SMTP.QUIT(v_connection_handle);
          DBMS_LOB.FREETEMPORARY(l_blob);
          DBMS_LOB.FILECLOSE(V_SRC_LOC);
         sssl_internal_error_track(sqlcode,sqlerrm,'SSSL_SEND_MAIL',l_filename||'-'||p_recipient);
      END;Kindly help me out to add the group or one or more mail id's
    Thanks in Advance
    Cheers,
    Shan.

    As, I said earlier (in some of your posts), you have to add UTL_RCPT for each recepient. For example, if you have CC list into some variable p_cc as comma separated list, then you can do something like this:
    ll_cc := '[email protected],[email protected],[email protected]';
       loop
           exit when l_to is null;
           n := instr( l_cc, ',' );
           IF n =0 THEN exit; end if;
           l_tmp := substr( l_cc, 1, n-1 );
           l_cc := substr( l_cc, n+1 );
           utl_smtp.rcpt( l_tmp );
       end loop;PS.: Not tested.
    Read this excellent explanation from Billy Verreynne
    To expand on what was said - the SMTP server does not parse the mail data you give it. It does not look at that to see the recipients, CC and BC list. The mail data is just that - raw data. This is the payload that the SMTP server will deliver.Where will it deliver it to? That you need to explicitly tell it via the RCPT TO SMTP command. So you need to build a complete list of names that includes all recipients, including CC and BC.
    And then use this list of names to instruct the SMTP SERVER, via the RCPT TO command, who must receive the message payload.>
    Re: Problem in sending email from oracle

  • UTL_SMTP mail with attachment( Problem in attaching zip file)

    Hi All,
    I used the below code for sending email with attachment. but when i try to add the message body its not working in the sense its not attaching my file. when i commented that line its attaching the file.
    commented lines:
    -- utl_smtp.write_data(mail_conn,UTL_TCP.CRLF ||'Body' ||':'|| text || UTL_TCP.CRLF);
      --utl_smtp.write_data(mail_conn,UTL_TCP.CRLF||text || UTL_TCP.CRLF );How to resolve this issue???
    Complete procedure.
    create or replace
    procedure sssl_send_mail (
          p_sender varchar2,
          p_recipient varchar2,
          p_cc varchar2,
          p_subject varchar2,
          p_filename varchar2,
          text varchar2) is    
        --c utl_smtp.connection;
        v_raw raw(57);
        v_length integer := 0;
        v_buffer_size integer := 57;
        v_offset integer := 1;
        mailhost    VARCHAR2(64) := 'xxxxxxxxxx';
        port constant number(2):=25;
        timeout number :=180;
        mail_conn  utl_smtp.connection;  
    p_blob Blob;
    temp_os_file bfile;
    ex number; 
    begin 
       DBMS_LOB.CREATETEMPORARY(p_blob,true);
       temp_os_file := BFILENAME ('xxxxxxxx',p_filename);
       ex := dbms_lob.fileexists(temp_os_file);
          if ex = 1 then
             dbms_lob.fileopen(temp_os_file, dbms_lob.file_readonly);
             dbms_lob.loadfromfile(p_blob,temp_os_file, dbms_lob.getlength(temp_os_file));
             dbms_lob.fileclose(temp_os_file);
           end if;
       mail_conn := utl_smtp.open_connection(mailhost, port,timeout);
       utl_smtp.helo(mail_conn, mailhost);
       utl_smtp.mail(mail_conn, p_sender);
       utl_smtp.rcpt(mail_conn, p_recipient);
       utl_smtp.rcpt(mail_conn, p_cc);
       utl_smtp.open_data(mail_conn);
      utl_smtp.write_data(mail_conn,'From'||':'|| p_sender || UTL_TCP.CRLF);
      utl_smtp.write_data(mail_conn,'To'||':'|| p_recipient || UTL_TCP.CRLF);
      utl_smtp.write_data(mail_conn,'CC'||':'|| p_cc || UTL_TCP.CRLF);
      utl_smtp.write_data(mail_conn,'Subject' ||':'|| p_subject || UTL_TCP.CRLF);
    -- utl_smtp.write_data(mail_conn,UTL_TCP.CRLF ||'Body' ||':'|| text || UTL_TCP.CRLF);
      --utl_smtp.write_data(mail_conn,UTL_TCP.CRLF||text || UTL_TCP.CRLF );
        utl_smtp.write_data( mail_conn, 'Content-Disposition: attachment; filename="' || p_filename || '"' || utl_tcp.crlf);
        utl_smtp.write_data( mail_conn, 'Content-Transfer-Encoding: base64' || utl_tcp.crlf );
        utl_smtp.write_data( mail_conn, utl_tcp.crlf );
        v_length := dbms_lob.getlength(p_blob);    
        <<while_loop>>
        while v_offset < v_length loop
          dbms_lob.read( p_blob, v_buffer_size, v_offset, v_raw );
          utl_smtp.write_raw_data( mail_conn, utl_encode.base64_encode(v_raw) );
          utl_smtp.write_data( mail_conn, utl_tcp.crlf );
          v_offset := v_offset + v_buffer_size;
        end loop while_loop;
        utl_smtp.write_data( mail_conn, utl_tcp.crlf );
        utl_smtp.close_data(mail_conn);
        utl_smtp.quit(mail_conn);
      exception
        when utl_smtp.transient_error or utl_smtp.permanent_error then
          utl_smtp.quit(mail_conn);
          raise;
        when others then
        raise;
      end;Please help me out to resolve this issue.
    Thanks in advance.
    Cheers ,
    Shan.
    Edited by: Shan on 13 Jan, 2011 1:08 PM
    Edited by: Shan on 14 Jan, 2011 3:22 PM

    Hi,
    Some Problems i am facing with this procedure. when i try to add CC in this procedure. Its sending the mail for recipent , But the CC person not getting the mail. How to resolve this??
    create or replace
    PROCEDURE sssl_send_mail( p_sender varchar2,
          p_recipient varchar2,
          p_cc varchar2,
          p_subject varchar2,
          p_filename varchar2,
          text varchar2) is 
        /*LOB operation related varriables */
       v_src_loc  BFILE;
       l_buffer   RAW(200);
       l_amount   BINARY_INTEGER := 200;
       l_pos      INTEGER := 1;
       l_blob     BLOB := EMPTY_BLOB;
       l_blob_len INTEGER;
       v_amount   INTEGER;
        /*UTL_SMTP related varriavles. */
        v_connection_handle  UTL_SMTP.CONNECTION;
        v_from_email_address VARCHAR2(200);
        v_to_email_address   VARCHAR2(200) ;
        v_smtp_host          VARCHAR2(10) ;
        v_subject            VARCHAR2(500) ;
        l_message            VARCHAR2(3000);
        /* This send_header procedure is written in the documentation */
        PROCEDURE send_header(pi_name IN VARCHAR2, pi_header IN VARCHAR2) AS
        BEGIN
        --dbms_output.put_line('entering into procedure');
        --dbms_output.put_line(pi_name || ': ' || pi_header);
          UTL_SMTP.WRITE_DATA(v_connection_handle,
                              pi_name || ': ' || pi_header || UTL_TCP.CRLF);
        END;
      BEGIN
       v_src_loc             := BFILENAME('BROKERREPORTS',p_filename);
       v_from_email_address  := p_sender;
       v_to_email_address    := p_recipient;
       v_smtp_host           := 'sbssld1'; --My mail server, replace it with yours.
       v_subject             := p_subject;
       l_message      := 'test';
        /*Preparing the LOB from file for attachment. */
        DBMS_LOB.OPEN(v_src_loc, DBMS_LOB.LOB_READONLY); --Read the file
        DBMS_LOB.CREATETEMPORARY(l_blob, TRUE); --Create temporary LOB to store the file.
        v_amount := DBMS_LOB.GETLENGTH(v_src_loc); --Amount to store.
        DBMS_LOB.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- Loading from file into temporary LOB
        l_blob_len := DBMS_LOB.getlength(l_blob);
    /*UTL_SMTP related coding. */
        v_connection_handle := UTL_SMTP.OPEN_CONNECTION(host => v_smtp_host);
        UTL_SMTP.HELO(v_connection_handle, v_smtp_host);
        UTL_SMTP.MAIL(v_connection_handle, v_from_email_address);
        UTL_SMTP.RCPT(v_connection_handle, v_to_email_address);
        UTL_SMTP.OPEN_DATA(v_connection_handle);
        send_header('From', v_from_email_address || '<'||'>');
        send_header('TO', v_to_email_address || '<'||'>');
        send_header('CC', p_cc        || '<'||'>');
        send_header('Subject', v_subject);
        --MIME header.
       UTL_SMTP.WRITE_DATA(v_connection_handle,
                          'MIME-Version: 1.0' || UTL_TCP.CRLF);
       UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Type: multipart/mixed; ' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' boundary= "' || 'SAUBHIK.SECBOUND' || '"' ||
                            UTL_TCP.CRLF);
       UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Mail Body
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Type: text/plain;' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' charset=US-ASCII' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, l_message || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Mail Attachment
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                           'Content-Type: application/octet-stream' ||
                            UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Disposition: attachment; ' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            ' filename="' || p_filename || '"' || --My filename
                            UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            'Content-Transfer-Encoding: base64' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
      /* Writing the BLOL in chunks */
        WHILE l_pos < l_blob_len LOOP
          DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
          UTL_SMTP.write_raw_data(v_connection_handle,
                                  UTL_ENCODE.BASE64_ENCODE(l_buffer));
          UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
          l_buffer := NULL;
          l_pos    := l_pos + l_amount;
        END LOOP;
        UTL_SMTP.WRITE_DATA(v_connection_handle, UTL_TCP.CRLF);
        -- Close Email
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            '--' || 'SAUBHIK.SECBOUND' || '--' || UTL_TCP.CRLF);
        UTL_SMTP.WRITE_DATA(v_connection_handle,
                            UTL_TCP.CRLF || '.' || UTL_TCP.CRLF);
        UTL_SMTP.CLOSE_DATA(v_connection_handle);
        UTL_SMTP.QUIT(v_connection_handle);
        DBMS_LOB.FREETEMPORARY(l_blob);
        DBMS_LOB.FILECLOSE(v_src_loc);
      EXCEPTION
        WHEN OTHERS THEN
          UTL_SMTP.QUIT(v_connection_handle);
          DBMS_LOB.FREETEMPORARY(l_blob);
          DBMS_LOB.FILECLOSE(v_src_loc);
         sssl_internal_error_track(sqlcode,sqlerrm,'SSSL_SEND_MAIL',NULL);
      END;Thanks in Advance.
    Cheers,
    Shan.
    Edited by: Shan on 13 Jan, 2011 12:33 PM
    Edited by: Shan on 13 Jan, 2011 1:05 PM

  • Sending email to multiple address using Utl_Smtp

    Hi,
    I want to send email to multiple address using the Utl_Smtp feature.
    When I am sending email to one email address in the To:Field it works fine.However, when I send
    to multiple address I am getting the below error.I am using a table(Email_test) to store all email id.
    Error report:
    ORA-29279: SMTP permanent error: 501 5.1.3 Invalid address
    ORA-06512: at "SYS.UTL_SMTP", line 20
    ORA-06512: at "SYS.UTL_SMTP", line 98
    ORA-06512: at "SYS.UTL_SMTP", line 240
    ORA-06512: at line 48
    29279. 00000 - "SMTP permanent error: %s"
    *Cause:    A SMTP permanent error occurred.
    *Action:   Correct the error and retry the SMTP operation.
    I am trying two options both ways I am getting error
    Option 1:
    Selecting two email id indivually in the select query as below
    select email into v_Recipient1 from Email_test where Key_name='U1';
    select email into v_Recipient2 from Email_test where Key_name='U2';
    v_Recipient := v_Recipient1||';'||v_Recipient2;
    Option 2:
    Is there a way to use option
    select email into v_Recipient1 from Email_test where Key_name='U4';
    ===========================================================
    Create table Script
    ===========================================================
    create table Email_test (Email varchar2(100),Key_name varchar2(10));
    insert into Email_test values ('[email protected]','U1');
    insert into Email_test values ('[email protected]','U2');
    insert into Email_test values ('[email protected]','U3')
    insert into Email_test values ('[email protected];[email protected];[email protected]','U4');
    select * from Email_test
    [email protected]                         U1
    [email protected]                         U2
    [email protected]                         U3
    [email protected];[email protected];[email protected]     U4
    select * from Email_test where Key_name in ('U1','U2','U3')
    [email protected]     U1
    [email protected]     U2
    [email protected]     U3
    select * from Email_test where Key_name='U4'
    [email protected];[email protected];[email protected]
    =======================================================
    PL/SQL Block
    ===========================================================
    declare
    v_From VARCHAR2(80) := '[email protected]';
    v_cc VARCHAR2(80);
    v_Recipient VARCHAR2(80) ;
    v_Recipient1 VARCHAR2(80) ;
    v_Recipient2 VARCHAR2(80) ;
    v_Subject VARCHAR2(80);
    v_Mail_Host VARCHAR2(50);
    v_Mail_Conn utl_smtp.Connection;
    crlf VARCHAR2(2) := chr(13)||chr(10);
    begin
    --Mail Host name
    select VALUE into v_Mail_Host from Server_info where server_name = 'SMTPServer';
    select email into v_Recipient1 from Email_test where Key_name='U1';
    select email into v_Recipient2 from Email_test where Key_name='U2';
    v_Recipient := v_Recipient1||';'||v_Recipient2;
    --for CC
    select email into v_cc from Email_test where Key_name='U3';
    v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
    utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
    utl_smtp.Mail(v_Mail_Conn, v_From);
    utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
    utl_smtp.Rcpt(v_Mail_Conn, v_cc); -- To CC recepient
    utl_smtp.Rcpt(v_Mail_Conn, v_BCC); To BCC recepient
    utl_smtp.Data(v_Mail_Conn,
    'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
    'From: ' || v_From || crlf ||
    'Subject: '|| v_Subject || crlf ||
    'To: ' || v_Recipient || crlf ||
    'Cc: ' || v_cc || crlf ||
    'Content-Type: text/html;' ||crlf ||
    --'Hello this is a test email');
    crlf || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf );
    utl_smtp.Quit(v_mail_conn);
    end;
    Any suggestion how to approach this issue.
    Thank you

    Simple Mail Transfer Protocol RFC 5321 specifications.
          RCPT TO:<forward-path> [ SP <rcpt-parameters> ] <CRLF>
       The first or only argument to this command includes a forward-path
       (normally a mailbox and domain, always surrounded by "&gt;" and "&lt;"
       brackets) identifying one recipient.In other words, you can only define a SINGLE mailbox address at a time. Multiple addresses requires multiple repeats of this command verb, once per maibox.
    Do not confuse this and the To: tag line in the Mime header that contains a comma delimited list of recipients. That tag line can contain anything - it is not parsed, not checked, and not verified as matching the actual recipient mailboxes as specified via the RCPT TO verb.

  • Sending email using UTL_SMTP

    Dear experts,
    I am trying to send an email using UTL_SMTP (i switched from UTL_MAIL to UTL_SMTP since i need to send mails with large attachments - BLOB). I am using the demo_mail package given here:
    http://www.oracle.com/technology/sample_code/tech/pl_sql/htdocs/Utl_Smtp_Sample.html
    While running the program I am getting an error message (from the error.log) saying:
    [Mon Aug 04 14:00:21 2008] [error] [client 192.168.0.7] mod_plsql: /ns/email_p.send HTTP-404 ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. 9sm12723808qbw.6\nORA-06512: at "SYS.UTL_SMTP", line 20\nORA-06512: at "SYS.UTL_SMTP", line 98\nORA-06512: at "SYS.UTL_SMTP", line 221\nORA-06512: at "NEXTSTEP.SEND_EMAIL_HELPER", line 258\nORA-06512: at "NEXTSTEP.SEND_EMAIL_HELPER", line 119\nORA-06512: at "NEXTSTEP.EMAIL_P", line 33\nORA-06512: at line 31\n, referer: file:///C:/Documents and Settings/Mayank/My Documents/Flex Builder 3/ns5/bin-debug/main.swf
    My code is as follows:
    PACKAGE DECLARATION. This is the DEMO_MAIL package posted under above link (I have renamed it).
    CREATE OR REPLACE PACKAGE NEXTSTEP.send_email_helper IS
      ----------------------- Customizable Section -----------------------
      -- Customize the SMTP host, port and your domain name below.
        smtp_host   VARCHAR2(256) := 'smtp.gmail.com';
        smtp_port   PLS_INTEGER   := 587;
        smtp_domain VARCHAR2(256) := null;
      -- Customize the signature that will appear in the email's MIME header.
      -- Useful for versioning.
      MAILER_ID   CONSTANT VARCHAR2(256) := 'Mailer by Oracle UTL_SMTP';
      --------------------- End Customizable Section ---------------------
      -- A unique string that demarcates boundaries of parts in a multi-part email
      -- The string should not appear inside the body of any part of the email.
      -- Customize this if needed or generate this randomly dynamically.
      BOUNDARY        CONSTANT VARCHAR2(256) := '-----7D81B75CCC90D2974F7A1CBD';
      FIRST_BOUNDARY  CONSTANT VARCHAR2(256) := '--' || BOUNDARY || utl_tcp.CRLF;
      LAST_BOUNDARY   CONSTANT VARCHAR2(256) := '--' || BOUNDARY || '--' ||
                                                  utl_tcp.CRLF;
      -- A MIME type that denotes multi-part email (MIME) messages.
      MULTIPART_MIME_TYPE CONSTANT VARCHAR2(256) := 'multipart/mixed; boundary="'||
                                                      BOUNDARY || '"';
      MAX_BASE64_LINE_WIDTH CONSTANT PLS_INTEGER   := 76 / 4 * 3;
      -- A simple email API for sending email in plain text in a single call.
      -- The format of an email address is one of these:
      --   someone@some-domain
      --   "Someone at some domain" <someone@some-domain>
      --   Someone at some domain <someone@some-domain>
      -- The recipients is a list of email addresses  separated by
      -- either a "," or a ";"
      PROCEDURE mail(sender     IN VARCHAR2,
             recipients IN VARCHAR2,
             subject    IN VARCHAR2,
             message    IN VARCHAR2);
      -- Extended email API to send email in HTML or plain text with no size limit.
      -- First, begin the email by begin_mail(). Then, call write_text() repeatedly
      -- to send email in ASCII piece-by-piece. Or, call write_mb_text() to send
      -- email in non-ASCII or multi-byte character set. End the email with
      -- end_mail().
      FUNCTION begin_mail(sender     IN VARCHAR2,
                  recipients IN VARCHAR2,
                  subject    IN VARCHAR2,
                  mime_type  IN VARCHAR2    DEFAULT 'text/plain',
                  priority   IN PLS_INTEGER DEFAULT NULL)
                  RETURN utl_smtp.connection;
      -- Write email body in ASCII
      PROCEDURE write_text(conn    IN OUT NOCOPY utl_smtp.connection,
                   message IN VARCHAR2);
      -- Write email body in non-ASCII (including multi-byte). The email body
      -- will be sent in the database character set.
      PROCEDURE write_mb_text(conn    IN OUT NOCOPY utl_smtp.connection,
                  message IN            VARCHAR2);
      -- Write email body in binary
      PROCEDURE write_raw(conn    IN OUT NOCOPY utl_smtp.connection,
                  message IN RAW);
      -- APIs to send email with attachments. Attachments are sent by sending
      -- emails in "multipart/mixed" MIME format. Specify that MIME format when
      -- beginning an email with begin_mail().
      -- Send a single text attachment.
      PROCEDURE attach_text(conn         IN OUT NOCOPY utl_smtp.connection,
                data         IN VARCHAR2,
                mime_type    IN VARCHAR2 DEFAULT 'text/plain',
                inline       IN BOOLEAN  DEFAULT TRUE,
                filename     IN VARCHAR2 DEFAULT NULL,
                    last         IN BOOLEAN  DEFAULT FALSE);
      -- Send a binary attachment. The attachment will be encoded in Base-64
      -- encoding format.
      PROCEDURE attach_base64(conn         IN OUT NOCOPY utl_smtp.connection,
                  data         IN RAW,
                  mime_type    IN VARCHAR2 DEFAULT 'application/octet',
                  inline       IN BOOLEAN  DEFAULT TRUE,
                  filename     IN VARCHAR2 DEFAULT NULL,
                  last         IN BOOLEAN  DEFAULT FALSE);
      -- Send an attachment with no size limit. First, begin the attachment
      -- with begin_attachment(). Then, call write_text repeatedly to send
      -- the attachment piece-by-piece. If the attachment is text-based but
      -- in non-ASCII or multi-byte character set, use write_mb_text() instead.
      -- To send binary attachment, the binary content should first be
      -- encoded in Base-64 encoding format using the demo package for 8i,
      -- or the native one in 9i. End the attachment with end_attachment.
      PROCEDURE begin_attachment(conn         IN OUT NOCOPY utl_smtp.connection,
                     mime_type    IN VARCHAR2 DEFAULT 'text/plain',
                     inline       IN BOOLEAN  DEFAULT TRUE,
                     filename     IN VARCHAR2 DEFAULT NULL,
                     transfer_enc IN VARCHAR2 DEFAULT NULL);
      -- End the attachment.
      PROCEDURE end_attachment(conn IN OUT NOCOPY utl_smtp.connection,
                   last IN BOOLEAN DEFAULT FALSE);
      -- End the email.
      PROCEDURE end_mail(conn IN OUT NOCOPY utl_smtp.connection);
      -- Extended email API to send multiple emails in a session for better
      -- performance. First, begin an email session with begin_session.
      -- Then, begin each email with a session by calling begin_mail_in_session
      -- instead of begin_mail. End the email with end_mail_in_session instead
      -- of end_mail. End the email session by end_session.
      FUNCTION begin_session RETURN utl_smtp.connection;
      -- Begin an email in a session.
      PROCEDURE begin_mail_in_session(conn       IN OUT NOCOPY utl_smtp.connection,
                      sender     IN VARCHAR2,
                      recipients IN VARCHAR2,
                      subject    IN VARCHAR2,
                      mime_type  IN VARCHAR2  DEFAULT 'text/plain',
                      priority   IN PLS_INTEGER DEFAULT NULL);
      -- End an email in a session.
      PROCEDURE end_mail_in_session(conn IN OUT NOCOPY utl_smtp.connection);
      -- End an email session.
      PROCEDURE end_session(conn IN OUT NOCOPY utl_smtp.connection);
    END;How should I solve the above error? Can anyone help me with my query please?
    If I add the code to start TLS it still gives me an error. If I add the code
    utl_smtp.command(conn,'STARTTLS');
    utl_smtp.helo(conn, smtp_domain);
    under email_sender_help package just before under begin_session function, it gives me an error saying:
    ORA-29279: SMTP permanent error: 503 5.5.1 EHLO/HELO first. s27sm2097329qbs.12
    So then if i enter the same code after:
    utl_smtp.command(conn,'STARTTLS');
    utl_smtp.helo(conn, smtp_domain);
    It gives me an error:
    ORA-29278: SMTP transient error: 421 Service not available
    Message was edited by:
    Monk
    Message was edited by:
    Monk

    look like rely turned off on the server..
    check this.. or ask your network guys..
    Go to Control Panel->Add or Remove Programs->Click on
    Add/Remove Wndows Components
    Check IIS check box.
    Select Internet Information Service (IIS) option and click on Details button
    Check whether SMTP Service is checked or not.
    If not selected then select SMTP check box.
    This process should be done on server.
    It will help out from ORA-29278: SMTP transient error: 421 Service not available problem.
    thanks

  • How to send email attachments in any format by utl_smtp?

    Hi,
    I am Using this Script for Sending Mail With an Attachment,i run this Script(Oracle 11g Database R1) and i got this Message "PL/SQL procedure successfully completed." but still i have not received the mail.
    Please advice me....
    set serveroutput on
    /* Formatted on 2010/06/03 21:26 (Formatter Plus v4.8.8) */
    DECLARE
    v_file_handle UTL_FILE.file_type;
    v_email_server VARCHAR2 (100) := 'Email_Server;
    v_conn UTL_SMTP.connection;
    v_port NUMBER := 25;
    v_reply UTL_SMTP.reply;
    v_msg VARCHAR2 (32767);
    v_line VARCHAR2 (1000);
    v_message VARCHAR2 (1000);
    b_connected BOOLEAN := FALSE;
    v_sender VARCHAR2 (50) := '************';
    v_strt_day varchar2(100);
    v_end_day varchar2(100);
    crlf VARCHAR2 (2) := CHR (13) || CHR (10);
    recpt VARCHAR2 (255) := '*********';
    slp PLS_INTEGER := 300;
    pdirpath VARCHAR2 (50) := 'D:\Mail_Process';
    pfilename VARCHAR2 (50) := 'Multifile.html';
    p_stat number     := 0;
    BEGIN
    /***** Check if the file exists ****/
    BEGIN
    v_file_handle := UTL_FILE.fopen (pdirpath, pfilename, 'R');
    EXCEPTION
    WHEN UTL_FILE.invalid_path
    THEN
    p_stat := 99;
    RETURN;
    WHEN OTHERS
    THEN
    p_stat := 99;
    RETURN;
    END;
    /***** Try to connect for three times, do sleep in between for 5minutes *****/
    FOR i IN 1 .. 3
    LOOP
    BEGIN
    --open the connection with the smtp server and do the handshake
    v_conn := UTL_SMTP.open_connection (v_email_server, v_port);
    v_reply := UTL_SMTP.helo (v_conn, v_email_server);
    IF 250 = v_reply.code
    THEN
    b_connected := TRUE;
    EXIT;
    END IF;
    EXCEPTION
    WHEN OTHERS
    THEN
    DBMS_LOCK.sleep (slp);
    END;
    END LOOP;
    IF b_connected = FALSE
    THEN
    p_stat := 99;
    RETURN;
    END IF;
    v_reply := UTL_SMTP.mail (v_conn, v_sender);
    IF 250 != v_reply.code
    THEN
    p_stat := 99;
    RETURN;
    END IF;
    v_reply := UTL_SMTP.rcpt (v_conn, recpt);
    IF 250 != v_reply.code
    THEN
    p_stat := 99;
    RETURN;
    END IF;
    UTL_SMTP.open_data (v_conn);
    v_message :=
    'Sample Email This is an auto generated mail.
    Please do not reply to this mail.'
    || CHR (10);
    v_msg :=
    'Date: '
    || TO_CHAR (SYSDATE, 'Mon DD yy hh24:mi:ss')
    || crlf
    || 'From: '
    || v_sender
    || crlf
    || 'Subject: '
    || 'Sample file'
    || crlf
    || 'To: '
    || recpt
    || crlf
    || 'Mime-Version: 1.0'
    || crlf
    || 'Content-Type: multipart/mixed;
    boundary="DMW.Boundary.605592468"'
    || crlf
    || ''
    || crlf
    || v_message
    || crlf
    || ''
    || crlf
    || '--DMW.Boundary.605592468'
    || crlf
    || 'Content-Type: text/plain;
    name="v_message.txt"; charset=US-ASCII'
    || crlf
    || 'Content-Disposition: inline; filename="v_message.txt"'
    || crlf
    || 'Content-Transfer-Encoding: 7bit'
    || crlf
    || ''
    || crlf
    || v_message
    || crlf
    || crlf
    || crlf;
    UTL_SMTP.write_data (v_conn, v_msg);
    /***** Prepare the attachment to be sent *****/
    v_msg :=
    crlf
    || '--DMW.Boundary.605592468'
    || crlf
    || 'Content-Type:
    application/octet-stream; name="'
    || pfilename
    || '"'
    || crlf
    || 'Content-Disposition: attachment; filename="'
    || pfilename
    || '"'
    || crlf
    || 'Content-Transfer-Encoding: 7bit'
    || crlf
    || crlf;
    UTL_SMTP.write_data (v_conn, v_msg);
    LOOP
    BEGIN
    UTL_FILE.get_line (v_file_handle, v_line);
    EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
    EXIT;
    END;
    v_msg := '*** truncated ***' || crlf;
    v_msg := v_line || crlf;
    UTL_SMTP.write_data (v_conn, v_msg);
    END LOOP;
    UTL_FILE.fclose (v_file_handle);
    v_msg := crlf;
    UTL_SMTP.write_data (v_conn, v_msg);
    v_msg := crlf || '--DMW.Boundary.605592468--' || crlf;
    UTL_SMTP.write_data (v_conn, v_msg);
    UTL_SMTP.close_data (v_conn);
    UTL_SMTP.quit (v_conn);
    EXCEPTION
    WHEN OTHERS
    THEN
    p_stat := 99;
    END;
    Thank You
    Shan

    I would recommend not to use home grown email solutions and use what we all use
    1. UTL_MAIL oracle package http://download-west.oracle.com/docs/cd/B13789_01/appdev.101/b10802/u_mail.htm
    or
    2. SMTP rock solid solution http://www.oracle.com/technology/sample_code/tech/pl_sql/htdocs/maildemo_sql.txt

  • Setting "Follow Up" flag in email thru utl_smtp

    Hi,
    Can anybody tell me, how can I set up "Follow up" flag with date in mail using utl_smtp.
    Any help is greatly appreciated.

    Unless my recollection of RFC822, the "Follow Up" flag is not part of the SMTP protocol. So you'd have to ask the vendor of whatever front-end application your email recipients use how that particular application wants the follow up flag specified and whether the vendor extended the SMTP protocol to allow that.
    Justin

  • How to use UTL_SMTP to send email with existing file attachment

    Hello! I am trying to create a pl/sql procedure that lets me send an email and include an existing file to a email address. So far, I found information on how to send a file and create an attachment with information I put in the procedure. This is NOT what I'm trying to do. I'm trying to send an email and include an attachment for a file that already exists. I need the pre-existing file to be sent to the email recipient.
    This is how far I've gotten, but this only allows me to CREATE an attachment with the information I put in it from the procedure. I got it from the following site:
    http://www.orafaq.com/wiki/Send_mail_from_PL/SQL
    DECLARE
       v_From       VARCHAR2(80) := '[email protected]';
       v_Recipient  VARCHAR2(80) := '[email protected]';
       v_Subject    VARCHAR2(80) := 'Weekly Invoice Report';
       v_Mail_Host  VARCHAR2(30) := 'mail.mycompany.com';
       v_Mail_Conn  utl_smtp.Connection;
       crlf         VARCHAR2(2)  := chr(13)||chr(10);
    BEGIN
      v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
      utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
      utl_smtp.Mail(v_Mail_Conn, v_From);
      utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
      utl_smtp.Data(v_Mail_Conn,
        'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
        'From: '   || v_From || crlf ||
        'Subject: '|| v_Subject || crlf ||
        'To: '     || v_Recipient || crlf ||
        'MIME-Version: 1.0'|| crlf ||     -- Use MIME mail standard
        'Content-Type: multipart/mixed;'|| crlf ||
        ' boundary="-----SECBOUND"'|| crlf ||
        crlf ||
        '-------SECBOUND'|| crlf ||
        'Content-Type: text/plain;'|| crlf ||
        'Content-Transfer_Encoding: 7bit'|| crlf ||
        crlf ||
        'This is a test'|| crlf ||     -- Message body
        'of the email attachment'|| crlf ||
        crlf ||
        '-------SECBOUND'|| crlf ||
        'Content-Type: text/plain;'|| crlf ||
        ' name="ap_inv_supplier_cc10.txt"'|| crlf ||
        'Content-Transfer_Encoding: 8bit'|| crlf ||
        'Content-Disposition: attachment;'|| crlf ||
        ' filename="ap_inv_supplier_cc10.txt"'|| crlf ||
        crlf ||
        'TXT,file,attachment'|| crlf ||     -- Content of attachment  (THIS IS MY PROBLEM!  I NEED TO BE ABLE TO ATTACH AN EXISTING FILE, NOT CREATE A NEW ONE)
        crlf ||
        '-------SECBOUND--'               -- End MIME mail
      utl_smtp.Quit(v_mail_conn);
    EXCEPTION
      WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
        raise_application_error(-20000, 'Unable to send mail: '||sqlerrm);
    END;
    /

    First, you must create a directory object
    create directory ORALOAD as '/home/ldcgroup/ldccbc/'
    /Study the Prerequisites in the link I posted above, or else you will not be able to read/write files from the above directory object
    "fname" relates to the file name that you read from.
    In the code below, it is also the name of the file that you are attaching.
    Although they can be different!
    l_Output is the contents of the file.
    declare
    vInHandle  utl_file.file_type;
    flen       number;
    bsize      number;
    ex         boolean;
    l_Output   raw(32767);
    fname      varchar2(30) := 'ap_inv_supplier_cc10.txt';
    vSender    varchar2(30) := '[email protected]';
    vRecip     varchar2(30) := '[email protected]';
    vSubj      varchar2(50) := 'Weekly Invoice Report';
    vAttach    varchar2(50) := 'ap_inv_supplier_cc10.txt';
    vMType     varchar2(30) := 'text/plain; charset=us-ascii';
    begin
      utl_file.fgetattr('ORALOAD', fname, ex, flen, bsize);
      vInHandle := utl_file.fopen('ORALOAD', fname, 'R');
      utl_file.get_raw (vInHandle, l_Output);
      utl_file.fclose(vInHandle);
      utl_mail.send_attach_raw(sender       => vSender
                              ,recipients   => vRecip
                              ,subject      => vsubj
                              ,attachment   => l_Output
                              ,att_inline   => false
                              ,att_filename => fname);
    end;
    /

Maybe you are looking for