成功透過ORACLE傳送郵件
測試環境:LINUX AS3 , ORACLE 9.0.2.4
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
PL/SQL Release 9.2.0.4.0 - Production
CORE 9.2.0.3.0 Production
TNS for Linux: Version 9.2.0.4.0 - Production
NLSRTL Version 9.2.0.4.0 - Production
SQL> select * from v$version;
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
MAIL伺服器為WIN2003,WINMAIL
1.保證ORACLE伺服器到MAIL伺服器網路暢通,25埠開啟
2.建立傳送郵件的procedure如下:
---------------------------------------
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) := 'mail server'; -- address or IP
lc_mail_conn utl_smtp.connection;
ls_subject varchar2(100);
ls_msg_body varchar2(20000);
ls_username varchar2(256) := 'usercode';
ls_password varchar2(256) := 'password';
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, ''); --這裡的'utl_smtp.rcpt(lc_mail_conn, '');--這裡的'utl_smtp.open_data(lc_mail_conn);
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;
3.執行傳送郵件:
exec send_mail(我我','this is a oracle test mail');
注意:以上過程如果在編譯中出現demo_base64.encode must be declared,請建立以下包和包體;
---------------------------------
CREATE OR REPLACE PACKAGE demo_base64 IS
-- Base64-encode a piece of binary data.
--
-- Note that this encode function does not split the encoded text into
-- multiple lines with no more than 76 bytes each as required by
-- the MIME standard.
--
FUNCTION encode(r IN RAW) RETURN VARCHAR2;
END;
------------------------------------
CREATE OR REPLACE PACKAGE BODY demo_base64 IS
TYPE vc2_table IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
map vc2_table;
-- Initialize the Base64 mapping
PROCEDURE init_map IS
BEGIN
map(0) :='A'; map(1) :='B'; map(2) :='C'; map(3) :='D'; map(4) :='E';
map(5) :='F'; map(6) :='G'; map(7) :='H'; map(8) :='I'; map(9):='J';
map(10):='K'; map(11):='L'; map(12):='M'; map(13):='N'; map(14):='O';
map(15):='P'; map(16):='Q'; map(17):='R'; map(18):='S'; map(19):='T';
map(20):='U'; map(21):='V'; map(22):='W'; map(23):='X'; map(24):='Y';
map(25):='Z'; map(26):='a'; map(27):='b'; map(28):='c'; map(29):='d';
map(30):='e'; map(31):='f'; map(32):='g'; map(33):='h'; map(34):='i';
map(35):='j'; map(36):='k'; map(37):='l'; map(38):='m'; map(39):='n';
map(40):='o'; map(41):='p'; map(42):='q'; map(43):='r'; map(44):='s';
map(45):='t'; map(46):='u'; map(47):='v'; map(48):='w'; map(49):='x';
map(50):='y'; map(51):='z'; map(52):='0'; map(53):='1'; map(54):='2';
map(55):='3'; map(56):='4'; map(57):='5'; map(58):='6'; map(59):='7';
map(60):='8'; map(61):='9'; map(62):='+'; map(63):='/';
END;
FUNCTION encode(r IN RAW) RETURN VARCHAR2 IS
i pls_integer;
x pls_integer;
y pls_integer;
v VARCHAR2(32767);
BEGIN
-- For every 3 bytes, split them into 4 6-bit units and map them to
-- the Base64 characters
i := 1;
WHILE ( i + 2 <= utl_raw.length(r) ) LOOP
x := to_number(utl_raw.substr(r, i, 1), '0X') * 65536 +
to_number(utl_raw.substr(r, i + 1, 1), '0X') * 256 +
to_number(utl_raw.substr(r, i + 2, 1), '0X');
y := floor(x / 262144); v := v || map(y); x := x - y * 262144;
y := floor(x / 4096); v := v || map(y); x := x - y * 4096;
y := floor(x / 64); v := v || map(y); x := x - y * 64;
v := v || map(x);
i := i + 3;
END LOOP;
-- Process the remaining bytes that has fewer than 3 bytes.
IF ( utl_raw.length(r) - i = 0) THEN
x := to_number(utl_raw.substr(r, i, 1), '0X');
y := floor(x / 4); v := v || map(y); x := x - y * 4;
x := x * 16; v := v || map(x);
v := v || '==';
ELSIF ( utl_raw.length(r) - i = 1) THEN
x := to_number(utl_raw.substr(r, i, 1), '0X') * 256 +
to_number(utl_raw.substr(r, i + 1, 1), '0X');
y := floor(x / 1024); v := v || map(y); x := x - y * 1024;
y := floor(x / 16); v := v || map(y); x := x - y * 16;
x := x * 4; v := v || map(x);
v := v || '=';
END IF;
RETURN v;
END;
BEGIN
init_map;
END;
--結束.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/703656/viewspace-912867/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle郵件傳送Oracle
- Jenkins 如何成功傳送郵件?Jenkins
- Oracle 傳送郵件的儲存過程Oracle儲存過程
- 怎麼透過mailx向內部郵件伺服器傳送郵件?(解答)AI伺服器
- 透過地址簿向聯絡人傳送病毒郵件!
- 功能較全的oracle傳送郵件過程Oracle
- 郵件傳送
- 傳送郵件
- 怎麼透過mailx向內部郵件伺服器傳送郵件?(疑問板)AI伺服器
- 用oracle傳送電子郵件Oracle
- oracle 傳送郵件 實現方法Oracle
- 用Oracle傳送郵件procedure (zt)Oracle
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- Azure : 通過 SendGrid 傳送郵件
- BI Publisher通過MuseMail傳送郵件AI
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- java傳送郵件Java
- Powershell郵件傳送
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- 利用Oracle資料庫傳送郵件Oracle資料庫
- JavaMail 保證郵件傳送成功的方法總結JavaAI
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- golang傳送郵件(qq郵箱)Golang
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- 定時傳送郵件
- thinkjs 傳送郵件JS
- 使用phpmailer傳送郵件PHPAI
- Ubuntu的郵件傳送Ubuntu
- python傳送郵件Python