通過Oracle發送Email

來源:互聯網
上載者:User
本篇是通過Oracle10g發送Email執行個體,該Procedure支援中文郵件、HTML代碼、多收件者。
在Procedure中p_receiver為收件者地址,多收件者以“,”(英文逗號)分割,
p_sub為郵件標題,p_txt為郵件內容(支援HTML)。代碼如下:

create or replace procedure bsd_sendemail (p_receiver varchar2,p_sub varchar2,p_txt varchar2)  is  p_user varchar2(30) := '';  p_pass varchar2(30) := '';  p_sendor varchar2(20) := 'Gnie';  p_server varchar2(20) := 'Mail_Server_IP';  p_port number := 25;  p_need_smtp number := 0;  p_subject varchar2(4000);  l_crlf varchar2(2) := utl_tcp.crlf;  l_sendoraddress varchar2(4000);  l_splite        varchar2(10) := '++';  boundary            constant varchar2(256) := '-----BYSUK';  first_boundary      constant varchar2(256) := '--' || boundary || l_crlf;  last_boundary       constant varchar2(256) := '--' || boundary || '--' || l_crlf;  multipart_mime_type constant varchar2(256) := 'multipart/mixed; boundary="' || boundary || '"';  type address_list is table of varchar2(100) index by binary_integer;  my_address_list address_list;  ---------------------------------------分割郵件地址----------------------------------------------  procedure p_splite_str(p_str varchar2,p_splite_flag int default 1) is    l_addr varchar2(254) := '';    l_len  int;    l_str  varchar2(4000);    j      int := 0; --表示郵件地址或者附件的個數  begin    /*處理內送郵件地址清單,包括去空格、將;轉換為,等*/    l_str := trim(rtrim(replace(replace(p_str, ';', ','), ' ', ''), ','));    l_len := length(l_str);    for i in 1 .. l_len loop      if substr(l_str, i, 1) <> ',' then        l_addr := l_addr || substr(l_str, i, 1);      else        j := j + 1;        if p_splite_flag = 1 then --表示處理郵件地址          --前後需要加上'<>',否則很多郵箱將不能發送郵件          l_addr := '<' || l_addr || '>';          --調用郵件發送過程          my_address_list(j) := l_addr;        end if;        l_addr := '';      end if;      if i = l_len then        j := j + 1;        if p_splite_flag = 1 then          --調用郵件發送過程          l_addr := '<' || l_addr || '>';          my_address_list(j) := l_addr;        end if;      end if;    end loop;  end;  ------------------------------------------------寫郵件標頭和郵件內容------------------------------------------  procedure write_data(p_conn   in out nocopy utl_smtp.connection,                       p_name   in varchar2,                       p_value  in varchar2,                       p_splite varchar2 default ':',                       p_crlf   varchar2 default l_crlf) is  begin    /* utl_raw.cast_to_raw 對解決中文亂碼問題很重要*/    utl_smtp.write_raw_data(p_conn, utl_raw.cast_to_raw(convert(p_name||p_splite||p_value ||p_crlf, 'ZHS16CGB231280')));  end;  ----------------------------------------寫mime郵件尾部-----------------------------------------------------  procedure end_boundary(conn in out nocopy utl_smtp.connection, last in boolean default false) is  begin    utl_smtp.write_data(conn, utl_tcp.crlf);    if (last) then      utl_smtp.write_data(conn, last_boundary);    end if;  end;  ---------------------------------------------真正發送郵件的過程--------------------------------------------  procedure p_email(p_sendoraddress2 varchar2, --發送地址                    p_receiveraddress2 varchar2) --接受地址   is    l_conn utl_smtp.connection; --定義串連  begin    /*初始化郵件伺服器資訊,串連郵件伺服器*/    l_conn := utl_smtp.open_connection(p_server, p_port);    utl_smtp.helo(l_conn, p_server);    /* smtp伺服器登入校正 */    if p_need_smtp = 1 then      utl_smtp.command(l_conn, 'AUTH LOGIN', '');      utl_smtp.command(l_conn, utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(p_user))));      utl_smtp.command(l_conn, utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(p_pass))));    end if;    /*設定發送地址和接收地址*/    utl_smtp.mail(l_conn, p_sendoraddress2);    utl_smtp.rcpt(l_conn, p_receiveraddress2);    /*設定郵件標頭*/    utl_smtp.open_data(l_conn);    /*設定日期*/    --write_data(l_conn, 'Date', to_char(sysdate-1/3, 'dd Mon yy hh24:mi:ss'));    /*設定發送人*/    write_data(l_conn, 'From', p_sendor);    /*設定接收人*/    write_data(l_conn, 'To', p_receiver);    /*設定郵件主題*/    select replace('=?GB2312?B?'||utl_raw.cast_to_varchar2(utl_encode.base64_encode(rawtohex(p_sub)))||'?=',utl_tcp.crlf,'') 
into p_subject from dual; write_data(l_conn, 'Subject', p_subject); write_data(l_conn, 'Content-Type', multipart_mime_type); utl_smtp.write_data(l_conn, utl_tcp.crlf); utl_smtp.write_data(l_conn, first_boundary); write_data(l_conn, 'Content-Type', 'text/html;charset=gb2312'); --單獨空一行,否則,本文內容不顯示 utl_smtp.write_data(l_conn, utl_tcp.crlf); /* 設定郵件內文 把分隔字元還原成chr(10)。這主要是為了shell中調用該過程,如果有多行,則先把多行的內容合并成一行,並用 l_splite分隔 然後用 l_crlf替換chr(10)。這一步是必須的,否則將不能發送郵件內文有多行的郵件 */ write_data(l_conn, '', replace(replace(p_txt, l_splite, chr(10)), chr(10), l_crlf), '', ''); end_boundary(l_conn); /*關閉資料寫入*/ utl_smtp.close_data(l_conn); /*關閉串連*/ utl_smtp.quit(l_conn); end; ---------------------------------------------主過程-----------------------------------------------------begin l_sendoraddress := '<' || p_sendor || '>'; p_splite_str(p_receiver);--處理郵件地址 for k in 1 .. my_address_list.count loop p_email(l_sendoraddress, my_address_list(k)); end loop; /*處理郵件地址,根據逗號分割郵件*/end;

在PL/SQL中測試一下它的效能如何:

到Outlook中看看郵件到了沒有:

哈哈!及時收到,通過它可以在底層發送相關資料資訊,希望對大家有用!

另,不知道11g有沒有直接發送Mail的函數了~

::原始碼下載::

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.