利用Oracle資料庫的UTL_SMTP發送HTML 郵件

來源:互聯網
上載者:User

標籤:io   os   ar   for   strong   資料   art   on   cti   

Ok, that looks hard, but if you use this procedure I wrote, its really quite easy, it does all of the work for you:create or replace procedure html_email(    p_to            in varchar2,    p_from          in varchar2,    p_subject       in varchar2,    p_text          in varchar2 default null,    p_html          in varchar2 default null,    p_smtp_hostname in varchar2,    p_smtp_portnum  in varchar2)is    l_boundary      varchar2(255) default ‘a1b2c3d4e3f2g1‘;    l_connection    utl_smtp.connection;    l_body_html     clob := empty_clob;  --This LOB will be the email message    l_offset        number;    l_ammount       number;    l_temp          varchar2(32767) default null;begin    l_connection := utl_smtp.open_connection( p_smtp_hostname, p_smtp_portnum );    utl_smtp.helo( l_connection, p_smtp_hostname );    utl_smtp.mail( l_connection, p_from );    utl_smtp.rcpt( l_connection, p_to );    l_temp := l_temp || ‘MIME-Version: 1.0‘ ||  chr(13) || chr(10);    l_temp := l_temp || ‘To: ‘ || p_to || chr(13) || chr(10);    l_temp := l_temp || ‘From: ‘ || p_from || chr(13) || chr(10);    l_temp := l_temp || ‘Subject: ‘ || p_subject || chr(13) || chr(10);    l_temp := l_temp || ‘Reply-To: ‘ || p_from ||  chr(13) || chr(10);    l_temp := l_temp || ‘Content-Type: multipart/alternative; boundary=‘ ||                          chr(34) || l_boundary ||  chr(34) || chr(13) ||                          chr(10);    ----------------------------------------------------    -- Write the headers    dbms_lob.createtemporary( l_body_html, false, 10 );    dbms_lob.write(l_body_html,length(l_temp),1,l_temp);    ----------------------------------------------------    -- Write the text boundary    l_offset := dbms_lob.getlength(l_body_html) + 1;    l_temp   := ‘--‘ || l_boundary || chr(13)||chr(10);    l_temp   := l_temp || ‘content-type: text/plain; charset=us-ascii‘ ||                   chr(13) || chr(10) || chr(13) || chr(10);    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);    ----------------------------------------------------    -- Write the plain text portion of the email    l_offset := dbms_lob.getlength(l_body_html) + 1;    dbms_lob.write(l_body_html,length(p_text),l_offset,p_text);    ----------------------------------------------------    -- Write the HTML boundary    l_temp   := chr(13)||chr(10)||chr(13)||chr(10)||‘--‘ || l_boundary ||                     chr(13) || chr(10);    l_temp   := l_temp || ‘content-type: text/html;‘ ||                    chr(13) || chr(10) || chr(13) || chr(10);    l_offset := dbms_lob.getlength(l_body_html) + 1;    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);    ----------------------------------------------------    -- Write the HTML portion of the message    l_offset := dbms_lob.getlength(l_body_html) + 1;    dbms_lob.write(l_body_html,length(p_html),l_offset,p_html);    ----------------------------------------------------    -- Write the final html boundary    l_temp   := chr(13) || chr(10) || ‘--‘ ||  l_boundary || ‘--‘ || chr(13);    l_offset := dbms_lob.getlength(l_body_html) + 1;    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);    ----------------------------------------------------    -- Send the email in 1900 byte chunks to UTL_SMTP    l_offset  := 1;    l_ammount := 1900;    utl_smtp.open_data(l_connection);    while l_offset < dbms_lob.getlength(l_body_html) loop        utl_smtp.write_data(l_connection,                            dbms_lob.substr(l_body_html,l_ammount,l_offset));        l_offset  := l_offset + l_ammount ;        l_ammount := least(1900,dbms_lob.getlength(l_body_html) - l_ammount);    end loop;    utl_smtp.close_data(l_connection);    utl_smtp.quit( l_connection );    dbms_lob.freetemporary(l_body_html);end;/show errors

利用Oracle資料庫的UTL_SMTP發送HTML 郵件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.