採用oracle過程發郵件

來源:互聯網
上載者:User

標籤:新浪郵箱   oracle   message   伺服器   收件者   

收件者只有一個的過程如下:

 

CREATE OR REPLACE PROCEDURE p_mail_sina(sender IN VARCHAR2,--發送人
recipient IN VARCHAR2,--接收人
subject IN VARCHAR2,--郵件主題
message IN VARCHAR2) IS --郵件內容
mailhost VARCHAR2(30) := ‘smtp.sina.com‘ ; --新浪郵箱伺服器
c utl_smtp.connection;
msg VARCHAR2(1000);
BEGIN
msg := ‘Date: ‘ || to_char(SYSDATE - 1, ‘dd mon yy hh24:mi:ss‘) ||
utl_tcp.crlf || ‘From: <‘ || sender || ‘>‘ || utl_tcp.crlf ||
‘subject: ‘ || subject || utl_tcp.crlf || ‘To: <‘ || recipient || ‘>‘ ||
utl_tcp.crlf || ‘‘ || utl_tcp.crlf || message;
c := utl_smtp.open_connection(mailhost, 25);
utl_smtp.command(c, ‘auth login‘); --1
utl_smtp.command(c, --2
utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(‘使用者名稱‘)))); --3 --發送信箱使用者名
utl_smtp.command(c, --4
utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(‘密碼‘))));--5 --發送郵箱密碼
utl_smtp.helo(c, mailhost);
utl_smtp.mail(c, sender);
utl_smtp.rcpt(c, recipient);
utl_smtp.open_data(c);
utl_smtp.write_raw_data(c, utl_raw.cast_to_raw(msg));
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 OTHERS THEN
NULL;
END;
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END p_mail_sina;

注意事項:

1.由於在測試中我採用的都是外網的方式發送內送郵件,因此必須加上用***圈出的這部分代碼進行認證。內網內部發送就不需要了,不過沒測試。

2. ***圈出的這部分代碼第三行和第五行填寫的內容分別是郵箱伺服器對應的信箱使用者名和密碼。

3.用於發送的郵箱的伺服器必須建立acl,主要是三個步驟

1)建立存取控制清單acl

BEGIN

DBMS_NETWORK_ACL_ADMIN.CREATE_ACL (
acl => ‘email_server_permissions.xml‘,
description => ‘Enables network permissions for the e-mail server‘,
principal => ‘database_user‘,--進行操作的資料庫使用者且使用者要大寫
is_grant => TRUE,
privilege => ‘connect‘);
END;

2)將ACL與郵件伺服器關聯(這一步,採用不同的郵箱伺服器就需要分別執行,如想用qq郵箱發送,那麼就必須換成qq郵箱伺服器執行一遍)

 

BEGIN
DBMS_NETWORK_ACL_ADMIN.assign_acl (
acl => ‘email_server_permissions.xml‘,
host => ‘smtp.sina.com‘, /*新浪的郵箱伺服器位址,如果是用qq作為發送郵件,

則用qq郵箱伺服器位址smtp.qq.com*/
lower_port => 25,
upper_port => NULL);
COMMIT;
END;

關聯後,可用dba_network_acls查看是否已經產生

 

SELECT host, lower_port, upper_port, acl FROM sys.dba_network_acls;

 

3)為執行的資料庫使用者授予串連郵件伺服器的許可權

 

 

BEGIN
dbms_network_acl_admin.add_privilege(
acl => ‘email_server_permissions.xml‘,
principal =>‘database_user‘,--進行操作的資料庫使用者且使用者要大寫

is_grant => TRUE,
privilege => ‘connect‘);
END;

採用dba_network_acl_privileges視圖查看授與權限

 

SELECT acl,
principal,
privilege,
is_grant,
TO_CHAR(start_date, ‘DD-MON-YYYY‘) AS start_date,
TO_CHAR(end_date, ‘DD-MON-YYYY‘) AS end_date
FROM sys.dba_network_acl_privileges;

 

4.utl_tcp.crlf作用:換行

注意在寫郵件標題,發送人,接收人,郵件內容時,各個之間一定要有換行,否則會無法發送或者是郵件內容為空白。

 

5.中文亂碼問題

utl_smtp.write_raw_data 用該過程替代utl_smtp.write_data可以解決中文亂碼的問題。

 

 

6.自己測試中發現如果採用qq郵箱發送郵件,則

utl_smtp.helo(c, mailhost);語句必須放在utl_smtp.command之前,否則無法發送,但是新浪郵箱則無所謂,這個不知何故。 ???


 

 

發給多個人

CREATE OR REPLACE PROCEDURE p_sendmailto_many(sender IN VARCHAR2,
recipient1 IN VARCHAR2,
recipient2 IN VARCHAR2,
recipient3 IN VARCHAR2,
subject IN VARCHAR2,
message IN VARCHAR2) IS
mailhost VARCHAR2(30) := ‘smtp.qq.com‘;
c utl_smtp.connection;
msg VARCHAR2(1000);
BEGIN
msg := ‘Date: ‘ || to_char(SYSDATE - 1, ‘dd mon yy hh24:mi:ss‘) ||
utl_tcp.crlf || ‘From: <‘ || sender || ‘>‘ || utl_tcp.crlf ||
‘subject: ‘ || subject || utl_tcp.crlf || ‘To: <‘ || recipient1 ||
‘>;<‘ || recipient2 || ‘>‘ || utl_tcp.crlf || ‘Cc: <‘ ||
recipient3 || ‘>‘ || utl_tcp.crlf || ‘‘ || utl_tcp.crlf ||
message;
c := utl_smtp.open_connection(mailhost, 25);
utl_smtp.helo(c, mailhost);
utl_smtp.command(c, ‘auth login‘);
utl_smtp.command(c,
utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(sender))));
utl_smtp.command(c,
utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(‘password‘))));
utl_smtp.mail(c, sender);
utl_smtp.rcpt(c, recipient1);
utl_smtp.rcpt(c, recipient2);
utl_smtp.rcpt(c, recipient3);
utl_smtp.open_data(c);
utl_smtp.write_raw_data(c, utl_raw.cast_to_raw(msg));
utl_smtp.close_data(c);
utl_smtp.quit(c);
END p_sendmailto_many;

本文出自 “kingdba” 部落格,請務必保留此出處http://kingdba.blog.51cto.com/801693/1441728

採用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.