oracle 發郵件 預存程序

來源:互聯網
上載者:User

標籤:郵件伺服器   使用者名稱   oracle   郵箱   密碼   

CREATE OR REPLACE PROCEDURE send_mail(

      p_recipient VARCHAR2, -- 郵件接收人

      p_subject   VARCHAR2, -- 郵件標題

      p_message   VARCHAR2  -- 郵件內文

)

IS


     --下面四個變數請根據實際郵件伺服器進行賦值

     v_mailhost  VARCHAR2(30) := ‘smtp.cheyipai.com‘;    --SMTP伺服器位址

     v_user      VARCHAR2(30) := ‘wangxuhui‘;            --登入SMTP伺服器的使用者名稱

     v_pass      VARCHAR2(20) := ‘TWEATcE3D‘;             --登入SMTP伺服器的密碼

     v_sender    VARCHAR2(50) := ‘[email protected]‘;    --寄件者郵箱,一般與 ps_user 對應


     v_conn  UTL_SMTP. connection ; --到郵件伺服器的串連

     v_msg varchar2(4000);  --郵件內容


BEGIN


     v_conn := UTL_SMTP.open_connection(v_mailhost, 25);

     UTL_SMTP.ehlo(v_conn, v_mailhost); --是用 ehlo() 而不是 helo() 函數

     --否則會報:ORA-29279: SMTP 永久性錯誤: 503 5.5.2 Send hello first.


     UTL_SMTP.command(v_conn, ‘AUTH LOGIN‘ );   -- smtp伺服器登入校正

     UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));

     UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));

     



     UTL_SMTP.mail(v_conn, v_sender);     --設定寄件者

     UTL_SMTP.rcpt(v_conn, p_recipient);  --設定收件者


     -- 建立要發送的郵件內容 注意前序資訊和郵件內文之間要空一行

     v_msg := ‘Date:‘ || TO_CHAR(SYSDATE, ‘dd mon yy hh24:mi:ss‘ )

         || UTL_TCP.CRLF || ‘From: ‘ || ‘<‘ || v_sender || ‘>‘

         || UTL_TCP.CRLF || ‘To: ‘ || ‘<‘ || p_recipient || ‘>‘

         || UTL_TCP.CRLF || ‘Subject: ‘ || p_subject

         || UTL_TCP.CRLF || UTL_TCP.CRLF  -- 這前面是前序資訊

         || p_message;    -- 這個是郵件內文


     UTL_SMTP.open_data(v_conn); --開啟流

     UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); --這樣寫標題和內容都能用中文

     UTL_SMTP.close_data(v_conn); --關閉流

     UTL_SMTP.quit(v_conn); --關閉串連


EXCEPTION


     WHEN OTHERS THEN

         DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);

         DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);


END send_mail;


本文出自 “先鋒的家園” 部落格,請務必保留此出處http://jackwxh.blog.51cto.com/2850597/1851957

oracle 發郵件 預存程序

聯繫我們

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