利用oracle預存程序發送郵件

來源:互聯網
上載者:User

CREATE OR REPLACE PROCEDURE SEND_MAIL
  (as_sender in varchar2, --郵件寄件者
  as_recp in varchar2, --郵件接收者
  as_subject in varchar2, --郵件標題
  as_msg_body in varchar2) --郵件內容
  IS
  ls_mailhost varchar2(30) := '***.***.***.***'; -- address or IP
  lc_mail_conn utl_smtp.connection;
  ls_subject varchar2(100);
  ls_msg_body varchar2(20000);
  ls_username varchar2(256) := 'zhangxf';
本文來自Sqlclub

  ls_password varchar2(256) := 'libozxf';
  BEGIN
  lc_mail_conn := utl_smtp.open_connection(ls_mailhost, 25);
  utl_smtp.helo(lc_mail_conn, ls_mailhost);
  utl_smtp.command(lc_mail_conn, 'AUTH LOGIN');
  utl_smtp.command(lc_mail_conn, demo_base64.encode(utl_raw.cast_to_raw(ls_username)));
  utl_smtp.command(lc_mail_conn, demo_base64.encode(utl_raw.cast_to_raw(ls_password)));
  ls_subject := 'Subject: [' || upper(sys_context('userenv', 'db_name')) || '] - ' || as_subject;
  ls_msg_body := as_msg_body;
  utl_smtp.mail(lc_mail_conn, '<'||as_sender||'>'); --這裡的'<' 一定要寫,不然會出現permanent error
  utl_smtp.rcpt(lc_mail_conn, '<'||as_recp||'>');--這裡的'<' 一定要寫,不然會出現permanent error
  utl_smtp.open_data(lc_mail_conn);
本文來自Sqlclub

  ls_msg_body := 'From: ' || as_sender || chr(13) || chr(10) || 'To: ' || as_recp || chr(13) || chr(10) || ls_subject ||
  chr(13) || chr(10) || chr(13) || chr(10) || ls_msg_body;
  utl_smtp.write_raw_data(lc_mail_conn, utl_raw.cast_to_raw(ls_msg_body)); --這樣寫subject可以支援中文但body內容不支援中文;
  -- utl_smtp.write_data(lc_mail_conn, ls_msg_body); --這樣寫subject不支援中文
  utl_smtp.close_data(lc_mail_conn);
  utl_smtp.quit(lc_mail_conn);
  EXCEPTION
  WHEN UTL_SMTP.INVALID_OPERATION THEN
  dbms_output.put_line('invalid operation');
  WHEN UTL_SMTP.TRANSIENT_ERROR THEN
  dbms_output.put_line('transient error');
  WHEN UTL_SMTP.PERMANENT_ERROR THEN
  dbms_output.put_line('permanent error');

  WHEN OTHERS THEN
  dbms_output.put_line('others');
  end send_mail;

  exec send_mail('zxf_feng@f163.com','zxf_feng@163.com','test','test');

 

相關文章

聯繫我們

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